Skip to content

Commit

Permalink
add ability to provide custom auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
jdolitsky committed Apr 28, 2018
1 parent ffe7123 commit 64aa920
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/helmpush/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
username string
password string
accessToken string
authHeader string
contextPath string
useHTTP bool
}
Expand Down Expand Up @@ -68,7 +69,8 @@ func newPushCmd(args []string) *cobra.Command {
f.StringVarP(&p.chartVersion, "version", "v", "", "Override chart version pre-push")
f.StringVarP(&p.username, "username", "u", "", "Override HTTP basic auth username [$HELM_REPO_USERNAME]")
f.StringVarP(&p.password, "password", "p", "", "Override HTTP basic auth password [$HELM_REPO_PASSWORD]")
f.StringVarP(&p.accessToken, "access-token", "", "", "Send token in authorization header [$HELM_REPO_ACCESS_TOKEN]")
f.StringVarP(&p.accessToken, "access-token", "", "", "Send token in Authorization header [$HELM_REPO_ACCESS_TOKEN]")
f.StringVarP(&p.authHeader, "auth-header", "", "", "Alternative header to use for token auth [$HELM_REPO_AUTH_HEADER]")
f.StringVarP(&p.contextPath, "context-path", "", "", "ChartMuseum context path [$HELM_REPO_CONTEXT_PATH]")
f.Parse(args)
return cmd
Expand All @@ -84,6 +86,9 @@ func (p *pushCmd) setFieldsFromEnv() {
if v, ok := os.LookupEnv("HELM_REPO_ACCESS_TOKEN"); ok && p.accessToken == "" {
p.accessToken = v
}
if v, ok := os.LookupEnv("HELM_REPO_AUTH_HEADER"); ok && p.authHeader == "" {
p.authHeader = v
}
if v, ok := os.LookupEnv("HELM_REPO_CONTEXT_PATH"); ok && p.contextPath == "" {
p.contextPath = v
}
Expand Down Expand Up @@ -131,6 +136,7 @@ func (p *pushCmd) push() error {
cm.Username(username),
cm.Password(password),
cm.AccessToken(p.accessToken),
cm.AuthHeader(p.authHeader),
cm.ContextPath(p.contextPath),
)

Expand Down Expand Up @@ -187,6 +193,7 @@ func (p *pushCmd) download(fileURL string) error {
cm.Username(p.username),
cm.Password(p.password),
cm.AccessToken(p.accessToken),
cm.AuthHeader(p.authHeader),
cm.ContextPath(p.contextPath),
)

Expand Down
6 changes: 5 additions & 1 deletion pkg/chartmuseum/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func (client *Client) DownloadFile(filePath string) (*http.Response, error) {
}

if client.opts.accessToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", client.opts.accessToken))
if client.opts.authHeader != "" {
req.Header.Set(client.opts.authHeader, client.opts.accessToken)
} else {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", client.opts.accessToken))
}
} else if client.opts.username != "" && client.opts.password != "" {
req.SetBasicAuth(client.opts.username, client.opts.password)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/chartmuseum/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type (
username string
password string
accessToken string
authHeader string
contextPath string
timeout time.Duration
}
Expand Down Expand Up @@ -47,6 +48,13 @@ func AccessToken(accessToken string) Option {
}
}

// AuthHeader is an alternative header to use for token auth
func AuthHeader(authHeader string) Option {
return func(opts *options) {
opts.authHeader = authHeader
}
}

// ContextPath is the URL prefix for ChartMuseum installation
func ContextPath(contextPath string) Option {
return func(opts *options) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/chartmuseum/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func (client *Client) UploadChartPackage(chartPackagePath string) (*http.Respons
}

if client.opts.accessToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", client.opts.accessToken))
if client.opts.authHeader != "" {
req.Header.Set(client.opts.authHeader, client.opts.accessToken)
} else {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", client.opts.accessToken))
}
} else if client.opts.username != "" && client.opts.password != "" {
req.SetBasicAuth(client.opts.username, client.opts.password)
}
Expand Down

0 comments on commit 64aa920

Please sign in to comment.