diff --git a/cmd/mstorectl/accountcmd.go b/cmd/mstorectl/accountcmd.go index 8330c1f..21ff13e 100644 --- a/cmd/mstorectl/accountcmd.go +++ b/cmd/mstorectl/accountcmd.go @@ -38,10 +38,11 @@ type AccountUtil struct { } type CommonAccountParams struct { - Username string - Password string - Hostname string - Timeout uint64 + Username string + Password string + Hostname string + Timeout uint64 + SkipTLSVerify bool } func (util *AccountUtil) CreateAccountCmds() *cobra.Command { @@ -52,10 +53,11 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command { } const defaultTimeout uint64 = 10 - subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Username, "user", "u", util.commonAccountParams.Username, "Username") - subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "p", util.commonAccountParams.Password, "Password") - subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "x", defaultHostname, "Hostname") - subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") + subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Username, "user", "U", util.commonAccountParams.Username, "Username") + subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "P", util.commonAccountParams.Password, "Password") + subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "X", defaultHostname, "Hostname") + subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "T", defaultTimeout, "Operation timeout") + subCmd.PersistentFlags().BoolVarP(&util.commonAccountParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify") subCmd.MarkFlagsRequiredTogether("user", "pass") vi := viper.New() @@ -90,8 +92,8 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command { Args: cobra.ExactArgs(2), Run: util.UpdateAccount, } - updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "N", "", "New username") - updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "P", "", "New password") + updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "u", "", "New username") + updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "p", "", "New password") updateAccountCmd.MarkFlagsOneRequired("newname", "newpass") subCmd.AddCommand(updateAccountCmd) @@ -102,7 +104,6 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command { Args: cobra.ExactArgs(2), Run: util.DeleteAccount, } - deleteAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username") subCmd.AddCommand(deleteAccountCmd) // ListAccount @@ -148,7 +149,7 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - accountID, err := client.NewClient().CreateAccount(ctx, hostname, params.NewUsername, params.NewPassword) + accountID, err := client.NewClient(common.SkipTLSVerify).CreateAccount(ctx, hostname, params.NewUsername, params.NewPassword) if err != nil { return res, err } @@ -162,7 +163,7 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea terms.RightReadImages, } for _, right := range fullRights { - id, err := client.NewClient().CreateGrantByAccountID(ctx, hostname, accountID, right, ".*") + id, err := client.NewClient(common.SkipTLSVerify).CreateGrantByAccountID(ctx, hostname, accountID, right, ".*") if err != nil { return res, err } @@ -202,9 +203,9 @@ func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *Upda re := regexp.MustCompile(uuidRegex) id := strings.ToLower(params.AccountID) if re.MatchString(id) { - err = client.NewClient().UpdateAccountByID(ctx, hostname, id, params.NewUsername, params.NewPassword) + err = client.NewClient(common.SkipTLSVerify).UpdateAccountByID(ctx, hostname, id, params.NewUsername, params.NewPassword) } else { - err = client.NewClient().UpdateAccountByName(ctx, hostname, params.AccountID, params.NewUsername, params.NewPassword) + err = client.NewClient(common.SkipTLSVerify).UpdateAccountByName(ctx, hostname, params.AccountID, params.NewUsername, params.NewPassword) } if err != nil { return res, err @@ -243,9 +244,9 @@ func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAcco re := regexp.MustCompile(uuidRegex) id := strings.ToLower(params.AccountID) if re.MatchString(id) { - opRes, err = client.NewClient().GetAccountByID(ctx, hostname, id) + opRes, err = client.NewClient(common.SkipTLSVerify).GetAccountByID(ctx, hostname, id) } else { - opRes, err = client.NewClient().GetAccountByName(ctx, hostname, params.AccountID) + opRes, err = client.NewClient(common.SkipTLSVerify).GetAccountByName(ctx, hostname, params.AccountID) } if err != nil { return res, err @@ -280,9 +281,9 @@ func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *Dele re := regexp.MustCompile(uuidRegex) id := strings.ToLower(params.AccountID) if re.MatchString(id) { - err = client.NewClient().DeleteAccountByID(ctx, hostname, id) + err = client.NewClient(common.SkipTLSVerify).DeleteAccountByID(ctx, hostname, id) } else { - err = client.NewClient().DeleteAccountByName(ctx, hostname, params.AccountID) + err = client.NewClient(common.SkipTLSVerify).DeleteAccountByName(ctx, hostname, params.AccountID) } if err != nil { return res, err @@ -327,7 +328,7 @@ func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListA timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - accounts, err := client.NewClient().ListAccounts(ctx, hostname) + accounts, err := client.NewClient(common.SkipTLSVerify).ListAccounts(ctx, hostname) if err != nil { return res, err } diff --git a/cmd/mstorectl/filecmd.go b/cmd/mstorectl/filecmd.go index 2bdaa86..a974288 100644 --- a/cmd/mstorectl/filecmd.go +++ b/cmd/mstorectl/filecmd.go @@ -41,6 +41,7 @@ func (util *FileUtil) CreateFileCmds() *cobra.Command { subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Username, "user", "u", "", "Username") subCmd.PersistentFlags().StringVarP(&util.commonFileParams.Password, "pass", "p", "", "Password") subCmd.PersistentFlags().Uint64VarP(&util.commonFileParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") + subCmd.PersistentFlags().BoolVarP(&util.commonFileParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify") subCmd.MarkPersistentFlagRequired("host") subCmd.MarkFlagsRequiredTogether("user", "pass") @@ -181,9 +182,10 @@ type FileUtil struct { } type CommonFileParams struct { - Username string - Password string - Timeout uint64 + Username string + Password string + Timeout uint64 + SkipTLSVerify bool } // FileInfo @@ -209,7 +211,7 @@ func (util *FileUtil) fileInfo(common *CommonFileParams, params *FileInfoParams) } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - exists, opres, err := client.NewClient().FileInfo(ctx, params.Filepath) + exists, opres, err := client.NewClient(common.SkipTLSVerify).FileInfo(ctx, params.Filepath) if err != nil { return res, err } @@ -244,7 +246,7 @@ func (util *FileUtil) putFile(common *CommonFileParams, params *PutFileParams) ( } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - err = client.NewClient().PutFile(ctx, params.Source, params.Dest) + err = client.NewClient(common.SkipTLSVerify).PutFile(ctx, params.Source, params.Dest) if err != nil { return res, err } @@ -275,7 +277,7 @@ func (util *FileUtil) getFile(common *CommonFileParams, params *GetFileParams) ( } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - _, err = client.NewClient().GetFile(ctx, params.Dest, params.Source) + _, err = client.NewClient(common.SkipTLSVerify).GetFile(ctx, params.Dest, params.Source) if err != nil { return res, err } @@ -303,7 +305,7 @@ func (util *FileUtil) deleteFile(common *CommonFileParams, params *DeleteFilePar } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - err = client.NewClient().DeleteFile(ctx, params.Filepath) + err = client.NewClient(common.SkipTLSVerify).DeleteFile(ctx, params.Filepath) if err != nil { return res, err } @@ -349,7 +351,7 @@ func (util *FileUtil) listFiles(common *CommonFileParams, params *ListFilesParam } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - files, err := client.NewClient().ListFiles(ctx, params.Filepath, pathUsage) + files, err := client.NewClient(common.SkipTLSVerify).ListFiles(ctx, params.Filepath, pathUsage) if err != nil { return res, err } @@ -413,7 +415,7 @@ func (util *FileUtil) importFiles(common *CommonFileParams, params *ImportFilesP putErrors = append(putErrors, err) return nil } - err = client.NewClient().PutFile(ctx, walkPath, dest) + err = client.NewClient(common.SkipTLSVerify).PutFile(ctx, walkPath, dest) if err != nil { putErrors = append(putErrors, err) fmt.Printf("- %s: error: %v \n", walkPath, err) @@ -475,7 +477,7 @@ func (util *FileUtil) exportFiles(common *CommonFileParams, params *ExportFilesP } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - files, err := client.NewClient().ListFiles(ctx, params.Filepath, pathUsage) + files, err := client.NewClient(common.SkipTLSVerify).ListFiles(ctx, params.Filepath, pathUsage) if err != nil { return res, err } @@ -494,7 +496,7 @@ func (util *FileUtil) exportFiles(common *CommonFileParams, params *ExportFilesP destpath := filepath.Join(params.Dest, file.Collection, file.Name) timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - _, err = client.NewClient().GetFile(ctx, srcpath, destpath) + _, err = client.NewClient(common.SkipTLSVerify).GetFile(ctx, srcpath, destpath) if err != nil { fmt.Printf("- %s: error %v\n", srcpath, err) //return res, err @@ -564,7 +566,7 @@ func (util *FileUtil) listCollections(common *CommonFileParams, params *ListColl } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - collecions, err := client.NewClient().ListCollections(ctx, params.Path, pathUsage) + collecions, err := client.NewClient(common.SkipTLSVerify).ListCollections(ctx, params.Path, pathUsage) if err != nil { return res, err } @@ -610,7 +612,7 @@ func (util *FileUtil) deleteCollection(common *CommonFileParams, params *DeleteC default: pathUsage = terms.AsFinePath } - files, err := client.NewClient().DeleteCollection(ctx, params.Path, pathUsage, params.DryRun) + files, err := client.NewClient(common.SkipTLSVerify).DeleteCollection(ctx, params.Path, pathUsage, params.DryRun) if err != nil { return res, err } diff --git a/cmd/mstorectl/grantcmd.go b/cmd/mstorectl/grantcmd.go index 854b361..82fb5f4 100644 --- a/cmd/mstorectl/grantcmd.go +++ b/cmd/mstorectl/grantcmd.go @@ -33,6 +33,7 @@ func (util *GrantUtil) CreateGrantCmds() *cobra.Command { subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Username, "user", "u", "", "Username") subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Password, "pass", "p", "", "Password") subCmd.PersistentFlags().Uint64VarP(&util.commonGrantParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") + subCmd.PersistentFlags().BoolVarP(&util.commonGrantParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify") vi := viper.New() vi.SetEnvPrefix("mstore") @@ -102,10 +103,11 @@ type GrantUtil struct { } type CommonGrantParams struct { - Username string - Password string - Hostname string - Timeout uint64 + Username string + Password string + Hostname string + Timeout uint64 + SkipTLSVerify bool } // CreateGrant @@ -140,9 +142,9 @@ func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGran id := strings.ToLower(params.AccountID) var operRes string if re.MatchString(id) { - operRes, err = client.NewClient().CreateGrantByAccountID(ctx, hostname, id, params.Right, params.Pattern) + operRes, err = client.NewClient(common.SkipTLSVerify).CreateGrantByAccountID(ctx, hostname, id, params.Right, params.Pattern) } else { - operRes, err = client.NewClient().CreateGrantByUsername(ctx, hostname, params.AccountID, params.Right, params.Pattern) + operRes, err = client.NewClient(common.SkipTLSVerify).CreateGrantByUsername(ctx, hostname, params.AccountID, params.Right, params.Pattern) } if err != nil { return res, err @@ -176,7 +178,7 @@ func (util *GrantUtil) updateGrant(common *CommonGrantParams, params *UpdateGran timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) id := strings.ToLower(params.GrantID) - err = client.NewClient().UpdateGrant(ctx, hostname, id, params.Pattern) + err = client.NewClient(common.SkipTLSVerify).UpdateGrant(ctx, hostname, id, params.Pattern) if err != nil { return res, err } @@ -212,7 +214,7 @@ func (util *GrantUtil) getGrant(common *CommonGrantParams, params *GetGrantParam opRes := &descr.Grant{} id := strings.ToLower(params.GrantID) - opRes, err = client.NewClient().GetGrant(ctx, hostname, id) + opRes, err = client.NewClient(common.SkipTLSVerify).GetGrant(ctx, hostname, id) if err != nil { return res, err } @@ -243,7 +245,7 @@ func (util *GrantUtil) deleteGrant(common *CommonGrantParams, params *DeleteGran timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) id := strings.ToLower(params.GrantID) - err = client.NewClient().DeleteGrant(ctx, hostname, id) + err = client.NewClient(common.SkipTLSVerify).DeleteGrant(ctx, hostname, id) if err != nil { return res, err } @@ -280,9 +282,9 @@ func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsP re := regexp.MustCompile(uuidRegex) id := strings.ToLower(params.AccountID) if re.MatchString(id) { - grants, err = client.NewClient().ListGrantsByAccountID(ctx, hostname, id) + grants, err = client.NewClient(common.SkipTLSVerify).ListGrantsByAccountID(ctx, hostname, id) } else { - grants, err = client.NewClient().ListGrantsByUsername(ctx, hostname, params.AccountID) + grants, err = client.NewClient(common.SkipTLSVerify).ListGrantsByUsername(ctx, hostname, params.AccountID) } if err != nil { return res, err diff --git a/cmd/mstorectl/imagecmd.go b/cmd/mstorectl/imagecmd.go index dc58529..79e7bdc 100644 --- a/cmd/mstorectl/imagecmd.go +++ b/cmd/mstorectl/imagecmd.go @@ -52,6 +52,7 @@ func (util *ImageUtil) CreateImageCmds() *cobra.Command { subCmd.PersistentFlags().StringVarP(&util.commonImageParams.Username, "user", "u", "", "Username") subCmd.PersistentFlags().StringVarP(&util.commonImageParams.Password, "pass", "p", "", "Password") subCmd.PersistentFlags().Uint64VarP(&util.commonImageParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") + subCmd.PersistentFlags().BoolVarP(&util.commonImageParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify") subCmd.MarkFlagsRequiredTogether("user", "pass") vi := viper.New() @@ -109,9 +110,10 @@ type ImageUtil struct { } type CommonImageParams struct { - Timeout uint64 - Username string - Password string + Timeout uint64 + Username string + Password string + SkipTLSVerify bool } // PushImage @@ -135,7 +137,7 @@ func (util *ImageUtil) pushImage(common *CommonImageParams, params *PushImagePar ctx := context.Background() res := &PushImageResult{} - cli := client.NewClient() + cli := client.NewClient(common.SkipTLSVerify) timeout := time.Duration(common.Timeout) * time.Second params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password) if err != nil { @@ -169,7 +171,7 @@ func (util *ImageUtil) imageInfo(common *CommonImageParams, params *ImageInfoPar res := &ImageInfoResult{} ctx := context.Background() - cli := client.NewClient() + cli := client.NewClient(common.SkipTLSVerify) timeout := time.Duration(common.Timeout) * time.Second params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password) @@ -209,7 +211,7 @@ func (util *ImageUtil) pullImage(common *CommonImageParams, params *PullImagePar ctx := context.Background() res := &PullImageResult{} - cli := client.NewClient() + cli := client.NewClient(common.SkipTLSVerify) timeout := time.Duration(common.Timeout) * time.Second params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password) if err != nil { @@ -249,7 +251,7 @@ func (util *ImageUtil) deleteImage(common *CommonImageParams, params *DeleteImag res := &DeleteImageResult{} ctx := context.Background() - cli := client.NewClient() + cli := client.NewClient(common.SkipTLSVerify) timeout := time.Duration(common.Timeout) * time.Second params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password) diff --git a/pkg/client/account.go b/pkg/client/account.go index bf89523..bbfa952 100644 --- a/pkg/client/account.go +++ b/pkg/client/account.go @@ -35,7 +35,7 @@ func (cli *Client) CreateAccount(ctx context.Context, hosturi, username, passwor if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apiuri, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apiuri, paramsJson) if err != nil { return res, err } @@ -69,7 +69,7 @@ func (cli *Client) GetAccountByID(ctx context.Context, hosturi, accountID string if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } @@ -102,7 +102,7 @@ func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username strin if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } @@ -136,7 +136,7 @@ func (cli *Client) UpdateAccountByID(ctx context.Context, hosturi string, accoun if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -167,7 +167,7 @@ func (cli *Client) UpdateAccountByName(ctx context.Context, hosturi, username, n if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -197,7 +197,7 @@ func (cli *Client) DeleteAccountByName(ctx context.Context, hosturi, username st if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -228,7 +228,7 @@ func (cli *Client) DeleteAccountByID(ctx context.Context, hosturi, accountID str if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -258,7 +258,7 @@ func (cli *Client) ListAccounts(ctx context.Context, hosturi string) ([]descr.Ac if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } diff --git a/pkg/client/client.go b/pkg/client/client.go index 7acb207..462b1bd 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -9,8 +9,12 @@ */ package client -type Client struct{} - -func NewClient() *Client { - return &Client{} +type Client struct { + skipTLSVerify bool +} + +func NewClient(skipTLSVerify bool) *Client { + return &Client{ + skipTLSVerify: skipTLSVerify, + } } diff --git a/pkg/client/file.go b/pkg/client/file.go index 44b16ab..aacce56 100644 --- a/pkg/client/file.go +++ b/pkg/client/file.go @@ -45,7 +45,7 @@ func (cli *Client) FileInfo(ctx context.Context, fileuri string) (bool, *descr.F basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return exists, file, err @@ -100,7 +100,7 @@ func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { @@ -135,7 +135,7 @@ func (cli *Client) GetFile(ctx context.Context, fileuri, filename string) (int64 basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return size, err @@ -195,7 +195,7 @@ func (cli *Client) DeleteFile(ctx context.Context, fileuri string) error { basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return err @@ -238,7 +238,7 @@ func (cli *Client) ListFiles(ctx context.Context, catalogURI, usePathAs string) basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return res, err @@ -307,7 +307,7 @@ func (cli *Client) ListCollections(ctx context.Context, catalogURI, usePathAs st basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return res, err @@ -378,7 +378,7 @@ func (cli *Client) DeleteCollection(ctx context.Context, catalogURI, usePathAs s basic := auxhttp.EncodeBasicAuth(username, password) req.Header.Add("Authorization", basic) } - client := makeHTTPClient() + client := makeHTTPClient(cli.skipTLSVerify) resp, err := client.Do(req) if err != nil { return res, err diff --git a/pkg/client/fileaux.go b/pkg/client/fileaux.go index eb23e90..d980c18 100644 --- a/pkg/client/fileaux.go +++ b/pkg/client/fileaux.go @@ -17,10 +17,10 @@ import ( "strings" ) -func makeHTTPClient() *http.Client { +func makeHTTPClient(skipTLSVerify bool) *http.Client { transport := &http.Transport{ TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: skipTLSVerify, }, } client := &http.Client{ diff --git a/pkg/client/grant.go b/pkg/client/grant.go index 679e683..f962d00 100644 --- a/pkg/client/grant.go +++ b/pkg/client/grant.go @@ -36,7 +36,7 @@ func (cli *Client) CreateGrantByAccountID(ctx context.Context, hosturi string, a if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apiuri, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apiuri, paramsJson) if err != nil { return res, err } @@ -71,7 +71,7 @@ func (cli *Client) CreateGrantByUsername(ctx context.Context, hosturi, username if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apiuri, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apiuri, paramsJson) if err != nil { return res, err } @@ -104,7 +104,7 @@ func (cli *Client) GetGrant(ctx context.Context, hosturi, grantID string) (*desc if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } @@ -137,7 +137,7 @@ func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -167,7 +167,7 @@ func (cli *Client) DeleteGrant(ctx context.Context, hosturi, grantID string) err if err != nil { return err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return err } @@ -199,7 +199,7 @@ func (cli *Client) ListGrantsByAccountID(ctx context.Context, hosturi, accountID if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } @@ -232,7 +232,7 @@ func (cli *Client) ListGrantsByUsername(ctx context.Context, hosturi, username s if err != nil { return res, err } - respBytes, err := doHTTPCall(ctx, apipath, paramsJson) + respBytes, err := doHTTPCall(ctx, cli.skipTLSVerify, apipath, paramsJson) if err != nil { return res, err } diff --git a/pkg/client/httpcall.go b/pkg/client/httpcall.go index a887f5c..fc0d5f6 100644 --- a/pkg/client/httpcall.go +++ b/pkg/client/httpcall.go @@ -41,7 +41,7 @@ func setApiPath(base, apipath string) (string, error) { } -func doHTTPCall(ctx context.Context, apiuri string, reqBytes []byte) ([]byte, error) { +func doHTTPCall(ctx context.Context, skipTLSVerify bool, apiuri string, reqBytes []byte) ([]byte, error) { var err error respBytes := make([]byte, 0) @@ -60,7 +60,7 @@ func doHTTPCall(ctx context.Context, apiuri string, reqBytes []byte) ([]byte, er httpReq.Header.Add("Authorization", basicHeader) } transport := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSVerify}, } httpClient := &http.Client{ Transport: transport, diff --git a/pkg/client/imageaux.go b/pkg/client/imageaux.go index dd177a4..ab551ce 100644 --- a/pkg/client/imageaux.go +++ b/pkg/client/imageaux.go @@ -16,11 +16,19 @@ import ( "strings" ) -type roundTripper struct{} +type RoundTripper struct { + skipTLSVerify bool +} -func (t *roundTripper) RoundTrip(r *http.Request) (*http.Response, error) { +func NewRoundTripper(skipTLSVerify bool) *RoundTripper { + return &RoundTripper{ + skipTLSVerify: skipTLSVerify, + } +} + +func (t *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { tlsConfig := &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: t.skipTLSVerify, } httpTransport := &http.Transport{ TLSClientConfig: tlsConfig, diff --git a/pkg/client/imagedelete.go b/pkg/client/imagedelete.go index 7a55c9e..0bab1ec 100644 --- a/pkg/client/imagedelete.go +++ b/pkg/client/imagedelete.go @@ -38,7 +38,7 @@ func (cli *Client) DeleteImage(ctx context.Context, imagepath string) error { } if username != "" && password != "" { - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) scopes := []string{repo.Scope(transport.PullScope)} regName := repo.RegistryStr() @@ -56,7 +56,7 @@ func (cli *Client) DeleteImage(ctx context.Context, imagepath string) error { } options = append(options, crane.WithTransport(authTransport)) } else { - transport := &roundTripper{} + transport := NewRoundTripper(cli.skipTLSVerify) options = append(options, crane.WithTransport(transport)) } diff --git a/pkg/client/imageinfo.go b/pkg/client/imageinfo.go index 11e2985..f71f9e8 100644 --- a/pkg/client/imageinfo.go +++ b/pkg/client/imageinfo.go @@ -47,7 +47,7 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr } if username != "" && password != "" { - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) scopes := []string{repo.Scope(transport.PullScope)} regName := repo.RegistryStr() @@ -65,7 +65,7 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr } options = append(options, crane.WithTransport(authTransport)) } else { - transport := &roundTripper{} + transport := NewRoundTripper(cli.skipTLSVerify) options = append(options, crane.WithTransport(transport)) } @@ -92,7 +92,7 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr remoteOptions = append(remoteOptions, remote.WithContext(ctx)) if username != "" && password != "" { - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) scopes := []string{repo.Scope(transport.PullScope)} regName := repo.RegistryStr() @@ -110,7 +110,7 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr } remoteOptions = append(remoteOptions, remote.WithTransport(authTransport)) } else { - transport := &roundTripper{} + transport := NewRoundTripper(cli.skipTLSVerify) options = append(options, crane.WithTransport(transport)) } diff --git a/pkg/client/imagepull.go b/pkg/client/imagepull.go index 27808b6..5ee19ef 100644 --- a/pkg/client/imagepull.go +++ b/pkg/client/imagepull.go @@ -42,7 +42,7 @@ func (cli *Client) PullImage(ctx context.Context, imagepath, filepath string) er if err != nil { return err } - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) scopes := []string{repo.Scope(transport.PullScope)} @@ -61,7 +61,7 @@ func (cli *Client) PullImage(ctx context.Context, imagepath, filepath string) er } options = append(options, crane.WithTransport(authTransport)) } else { - transport := &roundTripper{} + transport := NewRoundTripper(cli.skipTLSVerify) options = append(options, crane.WithTransport(transport)) } diff --git a/pkg/client/imagepush.go b/pkg/client/imagepush.go index 7b5f3ca..3131b9d 100644 --- a/pkg/client/imagepush.go +++ b/pkg/client/imagepush.go @@ -43,7 +43,7 @@ func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string) er if err != nil { return err } - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) scopes := []string{ repo.Scope(transport.PushScope), @@ -66,7 +66,7 @@ func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string) er } options = append(options, crane.WithTransport(authTransport)) } else { - defaultTransport := &roundTripper{} + defaultTransport := NewRoundTripper(cli.skipTLSVerify) options = append(options, crane.WithTransport(defaultTransport)) }