Skip to content

Commit 68d07f0

Browse files
Merge pull request #3323 from Infisical/feat/allowCustomHeadersOnCLI
Feat/allow custom headers on cli
2 parents c765c20 + 10a3658 commit 68d07f0

19 files changed

+343
-56
lines changed

cli/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/fatih/semgroup v1.2.0
1313
github.com/gitleaks/go-gitdiff v0.8.0
1414
github.com/h2non/filetype v1.1.3
15-
github.com/infisical/go-sdk v0.4.8
15+
github.com/infisical/go-sdk v0.5.1
1616
github.com/infisical/infisical-kmip v0.3.5
1717
github.com/mattn/go-isatty v0.0.20
1818
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a
@@ -34,6 +34,7 @@ require (
3434
golang.org/x/sys v0.31.0
3535
golang.org/x/term v0.30.0
3636
gopkg.in/yaml.v2 v2.4.0
37+
gopkg.in/yaml.v3 v3.0.1
3738
)
3839

3940
require (
@@ -125,7 +126,6 @@ require (
125126
google.golang.org/grpc v1.64.1 // indirect
126127
google.golang.org/protobuf v1.36.1 // indirect
127128
gopkg.in/ini.v1 v1.62.0 // indirect
128-
gopkg.in/yaml.v3 v3.0.1 // indirect
129129
)
130130

131131
require (

cli/go.sum

+3-3
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
277277
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
278278
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
279279
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
280-
github.com/infisical/go-sdk v0.4.8 h1:aphRnaauC5//PkP1ZbY9RSK2RiT1LjPS5o4CbX0x5OQ=
281-
github.com/infisical/go-sdk v0.4.8/go.mod h1:bMO9xSaBeXkDBhTIM4FkkREAfw2V8mv5Bm7lvo4+uDk=
280+
github.com/infisical/go-sdk v0.5.1 h1:bl0D4A6CmvfL8RwEQTcZh39nsxC6q3HSs76/4J8grWY=
281+
github.com/infisical/go-sdk v0.5.1/go.mod h1:ExjqFLRz7LSpZpGluqDLvFl6dFBLq5LKyLW7GBaMAIs=
282282
github.com/infisical/infisical-kmip v0.3.5 h1:QM3s0e18B+mYv3a9HQNjNAlbwZJBzXq5BAJM2scIeiE=
283283
github.com/infisical/infisical-kmip v0.3.5/go.mod h1:bO1M4YtKyutNg1bREPmlyZspC5duSR7hyQ3lPmLzrIs=
284284
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
@@ -858,4 +858,4 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
858858
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
859859
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
860860
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
861-
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
861+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

cli/packages/cmd/agent.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/Infisical/infisical-merge/packages/config"
3030
"github.com/Infisical/infisical-merge/packages/models"
3131
"github.com/Infisical/infisical-merge/packages/util"
32-
"github.com/go-resty/resty/v2"
3332
"github.com/spf13/cobra"
3433
)
3534

@@ -514,7 +513,10 @@ type NewAgentMangerOptions struct {
514513
}
515514

516515
func NewAgentManager(options NewAgentMangerOptions) *AgentManager {
517-
516+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
517+
if err != nil {
518+
util.HandleError(err, "Unable to get custom headers")
519+
}
518520
return &AgentManager{
519521
filePaths: options.FileDeposits,
520522
templates: options.Templates,
@@ -529,6 +531,7 @@ func NewAgentManager(options NewAgentMangerOptions) *AgentManager {
529531
SiteUrl: config.INFISICAL_URL,
530532
UserAgent: api.USER_AGENT, // ? Should we perhaps use a different user agent for the Agent for better analytics?
531533
AutoTokenRefresh: false,
534+
CustomHeaders: customHeaders,
532535
}),
533536
}
534537

@@ -716,7 +719,11 @@ func (tm *AgentManager) FetchNewAccessToken() error {
716719

717720
// Refreshes the existing access token
718721
func (tm *AgentManager) RefreshAccessToken() error {
719-
httpClient := resty.New()
722+
httpClient, err := util.GetRestyClientWithCustomHeaders()
723+
if err != nil {
724+
return err
725+
}
726+
720727
httpClient.SetRetryCount(10000).
721728
SetRetryMaxWaitTime(20 * time.Second).
722729
SetRetryWaitTime(5 * time.Second)

cli/packages/cmd/bootstrap.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/Infisical/infisical-merge/packages/api"
1212
"github.com/Infisical/infisical-merge/packages/util"
13-
"github.com/go-resty/resty/v2"
1413
"github.com/rs/zerolog/log"
1514
"github.com/spf13/cobra"
1615
)
@@ -70,8 +69,12 @@ var bootstrapCmd = &cobra.Command{
7069
return
7170
}
7271

73-
httpClient := resty.New().
74-
SetHeader("Accept", "application/json")
72+
httpClient, err := util.GetRestyClientWithCustomHeaders()
73+
if err != nil {
74+
log.Error().Msgf("Failed to get resty client with custom headers: %v", err)
75+
return
76+
}
77+
httpClient.SetHeader("Accept", "application/json")
7578

7679
bootstrapResponse, err := api.CallBootstrapInstance(httpClient, api.BootstrapInstanceRequest{
7780
Domain: util.AppendAPIEndpoint(domain),

cli/packages/cmd/dynamic_secrets.go

+50-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
// "github.com/Infisical/infisical-merge/packages/models"
1515
"github.com/Infisical/infisical-merge/packages/util"
1616
// "github.com/Infisical/infisical-merge/packages/visualize"
17-
"github.com/go-resty/resty/v2"
1817
"github.com/posthog/posthog-go"
1918
"github.com/spf13/cobra"
2019

@@ -56,7 +55,10 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) {
5655
}
5756

5857
var infisicalToken string
59-
httpClient := resty.New()
58+
httpClient, err := util.GetRestyClientWithCustomHeaders()
59+
if err != nil {
60+
util.HandleError(err, "Unable to get resty client with custom headers")
61+
}
6062

6163
if projectId == "" {
6264
workspaceFile, err := util.GetWorkSpaceFromFile()
@@ -85,10 +87,16 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) {
8587

8688
httpClient.SetAuthToken(infisicalToken)
8789

90+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
91+
if err != nil {
92+
util.HandleError(err, "Unable to get custom headers")
93+
}
94+
8895
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
8996
SiteUrl: config.INFISICAL_URL,
9097
UserAgent: api.USER_AGENT,
9198
AutoTokenRefresh: false,
99+
CustomHeaders: customHeaders,
92100
})
93101
infisicalClient.Auth().SetAccessToken(infisicalToken)
94102

@@ -164,7 +172,10 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
164172
}
165173

166174
var infisicalToken string
167-
httpClient := resty.New()
175+
httpClient, err := util.GetRestyClientWithCustomHeaders()
176+
if err != nil {
177+
util.HandleError(err, "Unable to get resty client with custom headers")
178+
}
168179

169180
if projectId == "" {
170181
workspaceFile, err := util.GetWorkSpaceFromFile()
@@ -193,10 +204,16 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
193204

194205
httpClient.SetAuthToken(infisicalToken)
195206

207+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
208+
if err != nil {
209+
util.HandleError(err, "Unable to get custom headers")
210+
}
211+
196212
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
197213
SiteUrl: config.INFISICAL_URL,
198214
UserAgent: api.USER_AGENT,
199215
AutoTokenRefresh: false,
216+
CustomHeaders: customHeaders,
200217
})
201218
infisicalClient.Auth().SetAccessToken(infisicalToken)
202219

@@ -286,7 +303,10 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
286303
}
287304

288305
var infisicalToken string
289-
httpClient := resty.New()
306+
httpClient, err := util.GetRestyClientWithCustomHeaders()
307+
if err != nil {
308+
util.HandleError(err, "Unable to get resty client with custom headers")
309+
}
290310

291311
if projectId == "" {
292312
workspaceFile, err := util.GetWorkSpaceFromFile()
@@ -315,10 +335,16 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
315335

316336
httpClient.SetAuthToken(infisicalToken)
317337

338+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
339+
if err != nil {
340+
util.HandleError(err, "Unable to get custom headers")
341+
}
342+
318343
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
319344
SiteUrl: config.INFISICAL_URL,
320345
UserAgent: api.USER_AGENT,
321346
AutoTokenRefresh: false,
347+
CustomHeaders: customHeaders,
322348
})
323349
infisicalClient.Auth().SetAccessToken(infisicalToken)
324350

@@ -384,7 +410,10 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
384410
}
385411

386412
var infisicalToken string
387-
httpClient := resty.New()
413+
httpClient, err := util.GetRestyClientWithCustomHeaders()
414+
if err != nil {
415+
util.HandleError(err, "Unable to get resty client with custom headers")
416+
}
388417

389418
if projectId == "" {
390419
workspaceFile, err := util.GetWorkSpaceFromFile()
@@ -413,10 +442,16 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
413442

414443
httpClient.SetAuthToken(infisicalToken)
415444

445+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
446+
if err != nil {
447+
util.HandleError(err, "Unable to get custom headers")
448+
}
449+
416450
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
417451
SiteUrl: config.INFISICAL_URL,
418452
UserAgent: api.USER_AGENT,
419453
AutoTokenRefresh: false,
454+
CustomHeaders: customHeaders,
420455
})
421456
infisicalClient.Auth().SetAccessToken(infisicalToken)
422457

@@ -481,7 +516,10 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
481516
}
482517

483518
var infisicalToken string
484-
httpClient := resty.New()
519+
httpClient, err := util.GetRestyClientWithCustomHeaders()
520+
if err != nil {
521+
util.HandleError(err, "Unable to get resty client with custom headers")
522+
}
485523

486524
if projectId == "" {
487525
workspaceFile, err := util.GetWorkSpaceFromFile()
@@ -510,10 +548,16 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
510548

511549
httpClient.SetAuthToken(infisicalToken)
512550

551+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
552+
if err != nil {
553+
util.HandleError(err, "Unable to get custom headers")
554+
}
555+
513556
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
514557
SiteUrl: config.INFISICAL_URL,
515558
UserAgent: api.USER_AGENT,
516559
AutoTokenRefresh: false,
560+
CustomHeaders: customHeaders,
517561
})
518562
infisicalClient.Auth().SetAccessToken(infisicalToken)
519563

cli/packages/cmd/init.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/Infisical/infisical-merge/packages/api"
1111
"github.com/Infisical/infisical-merge/packages/models"
1212
"github.com/Infisical/infisical-merge/packages/util"
13-
"github.com/go-resty/resty/v2"
1413
"github.com/manifoldco/promptui"
1514
"github.com/posthog/posthog-go"
1615
"github.com/rs/zerolog/log"
@@ -50,7 +49,10 @@ var initCmd = &cobra.Command{
5049
util.PrintErrorMessageAndExit("Your login session has expired, please run [infisical login] and try again")
5150
}
5251

53-
httpClient := resty.New()
52+
httpClient, err := util.GetRestyClientWithCustomHeaders()
53+
if err != nil {
54+
util.HandleError(err, "Unable to get resty client with custom headers")
55+
}
5456
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
5557

5658
organizationResponse, err := api.CallGetAllOrganizations(httpClient)
@@ -81,7 +83,10 @@ var initCmd = &cobra.Command{
8183
for i < 6 {
8284
mfaVerifyCode := askForMFACode(tokenResponse.MfaMethod)
8385

84-
httpClient := resty.New()
86+
httpClient, err := util.GetRestyClientWithCustomHeaders()
87+
if err != nil {
88+
util.HandleError(err, "Unable to get resty client with custom headers")
89+
}
8590
httpClient.SetAuthToken(tokenResponse.Token)
8691
verifyMFAresponse, mfaErrorResponse, requestError := api.CallVerifyMfaToken(httpClient, api.VerifyMfaTokenRequest{
8792
Email: userCreds.UserCredentials.Email,

cli/packages/cmd/login.go

+30-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/Infisical/infisical-merge/packages/srp"
2828
"github.com/Infisical/infisical-merge/packages/util"
2929
"github.com/fatih/color"
30-
"github.com/go-resty/resty/v2"
3130
"github.com/manifoldco/promptui"
3231
"github.com/posthog/posthog-go"
3332
"github.com/rs/cors"
@@ -178,10 +177,16 @@ var loginCmd = &cobra.Command{
178177
return
179178
}
180179

180+
customHeaders, err := util.GetInfisicalCustomHeadersMap()
181+
if err != nil {
182+
util.HandleError(err, "Unable to get custom headers")
183+
}
184+
181185
infisicalClient := infisicalSdk.NewInfisicalClient(context.Background(), infisicalSdk.Config{
182186
SiteUrl: config.INFISICAL_URL,
183187
UserAgent: api.USER_AGENT,
184188
AutoTokenRefresh: false,
189+
CustomHeaders: customHeaders,
185190
})
186191

187192
loginMethod, err := cmd.Flags().GetString("method")
@@ -359,7 +364,10 @@ func cliDefaultLogin(userCredentialsToBeStored *models.UserCredentials) {
359364
for i < 6 {
360365
mfaVerifyCode := askForMFACode("email")
361366

362-
httpClient := resty.New()
367+
httpClient, err := util.GetRestyClientWithCustomHeaders()
368+
if err != nil {
369+
util.HandleError(err, "Unable to get resty client with custom headers")
370+
}
363371
httpClient.SetAuthToken(loginTwoResponse.Token)
364372
verifyMFAresponse, mfaErrorResponse, requestError := api.CallVerifyMfaToken(httpClient, api.VerifyMfaTokenRequest{
365373
Email: email,
@@ -726,7 +734,10 @@ func askForLoginCredentials() (email string, password string, err error) {
726734

727735
func getFreshUserCredentials(email string, password string) (*api.GetLoginOneV2Response, *api.GetLoginTwoV2Response, error) {
728736
log.Debug().Msg(fmt.Sprint("getFreshUserCredentials: ", "email", email, "password: ", password))
729-
httpClient := resty.New()
737+
httpClient, err := util.GetRestyClientWithCustomHeaders()
738+
if err != nil {
739+
return nil, nil, err
740+
}
730741
httpClient.SetRetryCount(5)
731742

732743
params := srp.GetParams(4096)
@@ -776,7 +787,10 @@ func getFreshUserCredentials(email string, password string) (*api.GetLoginOneV2R
776787
func GetJwtTokenWithOrganizationId(oldJwtToken string, email string) string {
777788
log.Debug().Msg(fmt.Sprint("GetJwtTokenWithOrganizationId: ", "oldJwtToken", oldJwtToken))
778789

779-
httpClient := resty.New()
790+
httpClient, err := util.GetRestyClientWithCustomHeaders()
791+
if err != nil {
792+
util.HandleError(err, "Unable to get resty client with custom headers")
793+
}
780794
httpClient.SetAuthToken(oldJwtToken)
781795

782796
organizationResponse, err := api.CallGetAllOrganizations(httpClient)
@@ -811,7 +825,10 @@ func GetJwtTokenWithOrganizationId(oldJwtToken string, email string) string {
811825
for i < 6 {
812826
mfaVerifyCode := askForMFACode(selectedOrgRes.MfaMethod)
813827

814-
httpClient := resty.New()
828+
httpClient, err := util.GetRestyClientWithCustomHeaders()
829+
if err != nil {
830+
util.HandleError(err, "Unable to get resty client with custom headers")
831+
}
815832
httpClient.SetAuthToken(selectedOrgRes.Token)
816833
verifyMFAresponse, mfaErrorResponse, requestError := api.CallVerifyMfaToken(httpClient, api.VerifyMfaTokenRequest{
817834
Email: email,
@@ -913,7 +930,14 @@ func askToPasteJwtToken(success chan models.UserCredentials, failure chan error)
913930
}
914931

915932
// verify JTW
916-
httpClient := resty.New().
933+
httpClient, err := util.GetRestyClientWithCustomHeaders()
934+
if err != nil {
935+
failure <- err
936+
fmt.Println("Error getting resty client with custom headers", err)
937+
os.Exit(1)
938+
}
939+
940+
httpClient.
917941
SetAuthToken(userCredentials.JTWToken).
918942
SetHeader("Accept", "application/json")
919943

0 commit comments

Comments
 (0)