Skip to content

Commit

Permalink
Merge pull request #214 from Praqma/feature/basic-auth-repos
Browse files Browse the repository at this point in the history
support basic auth for helm repos. fixes #211
  • Loading branch information
Sami Alajrami authored Mar 12, 2019
2 parents 0f9c5cd + 4198c7d commit 42c3c5d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
incubator = "http://storage.googleapis.com/kubernetes-charts-incubator"
# myS3repo = "s3://my-S3-private-repo/charts"
# myGCSrepo = "gs://my-GCS-private-repo/charts"
# custom = "https://user:[email protected]"


# define the desired state of your applications helm charts
Expand Down
1 change: 1 addition & 0 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ helmRepos:
incubator: "http://storage.googleapis.com/kubernetes-charts-incubator"
#myS3repo: "s3://my-S3-private-repo/charts"
#myGCSrepo: "gs://my-GCS-private-repo/charts"
#custom: "https://user:[email protected]"

# define the desired state of your applications helm charts
# each contains the following:
Expand Down
21 changes: 18 additions & 3 deletions helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"log"
"net/url"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -291,16 +292,30 @@ func waitForTiller(namespace string) {
// Helm does not mind if a repo with the same name exists. It treats it as an update.
func addHelmRepos(repos map[string]string) (bool, string) {

for repoName, url := range repos {
for repoName, repoLink := range repos {
basicAuth := ""
// check if repo is in GCS, then perform GCS auth -- needed for private GCS helm repos
// failed auth would not throw an error here, as it is possible that the repo is public and does not need authentication
if strings.HasPrefix(url, "gs://") {
if strings.HasPrefix(repoLink, "gs://") {
gcs.Auth()
}

u, err := url.Parse(repoLink)
if err != nil {
logError("ERROR: failed to add helm repo: " + err.Error())
}
if u.User != nil {
p, ok := u.User.Password()
if !ok {
logError("ERROR: helm repo " + repoName + " has incomplete basic auth info. Missing the password!")
}
basicAuth = " --username " + u.User.Username() + " --password " + p

}

cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm repo add " + repoName + " " + strconv.Quote(url)},
Args: []string{"-c", "helm repo add " + basicAuth + " " + repoName + " " + strconv.Quote(repoLink)},
Description: "adding repo " + repoName,
}

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var destroy bool
var showDiff bool
var suppressDiffSecrets bool

const stableHelmRepo = "https://kubernetes-charts.storage.googleapis.com"
const incubatorHelmRepo = "http://storage.googleapis.com/kubernetes-charts-incubator"

func main() {
// set the kubecontext to be used Or create it if it does not exist
if !setKubeContext(s.Settings.KubeContext) {
Expand Down
4 changes: 0 additions & 4 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ func (s state) validate() (bool, string) {
}

// repos
if s.HelmRepos == nil || len(s.HelmRepos) == 0 {
return false, "ERROR: repos validation failed -- I need at least one helm repo " +
"to work with!"
}
for k, v := range s.HelmRepos {
_, err := url.ParseRequestURI(v)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func Test_state_validate(t *testing.T) {
HelmRepos: nil,
Apps: make(map[string]*release),
},
want: false,
want: true,
}, {
name: "test case 18 -- helmRepos/empty",
fields: fields{
Expand All @@ -379,7 +379,7 @@ func Test_state_validate(t *testing.T) {
HelmRepos: map[string]string{},
Apps: make(map[string]*release),
},
want: false,
want: true,
}, {
name: "test case 19 -- helmRepos/empty_repo_value",
fields: fields{
Expand Down
22 changes: 20 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func fromTOML(file string, s *state) (bool, string) {
if _, err := toml.Decode(tomlFile, s); err != nil {
return false, err.Error()
}

addDefaultHelmRepos(s)
resolvePaths(file, s)

return true, "INFO: Parsed TOML [[ " + file + " ]] successfully and found [ " + strconv.Itoa(len(s.Apps)) + " ] apps."
Expand Down Expand Up @@ -90,7 +90,7 @@ func fromYAML(file string, s *state) (bool, string) {
if err = yaml.UnmarshalStrict(yamlFile, s); err != nil {
return false, err.Error()
}

addDefaultHelmRepos(s)
resolvePaths(file, s)

return true, "INFO: Parsed YAML [[ " + file + " ]] successfully and found [ " + strconv.Itoa(len(s.Apps)) + " ] apps."
Expand Down Expand Up @@ -151,6 +151,24 @@ func stringInSlice(a string, list []string) bool {
return false
}

// addDefaultHelmRepos adds stable and incubator helm repos to the state if they are not already defined
func addDefaultHelmRepos(s *state) {
if s.HelmRepos == nil || len(s.HelmRepos) == 0 {
s.HelmRepos = map[string]string{
"stable": stableHelmRepo,
"incubator": incubatorHelmRepo,
}
log.Println("INFO: no helm repos provided, using the default 'stable' and 'incubator' repos.")
}
if _, ok := s.HelmRepos["stable"]; !ok {
s.HelmRepos["stable"] = stableHelmRepo
}
if _, ok := s.HelmRepos["incubator"]; !ok {
s.HelmRepos["incubator"] = incubatorHelmRepo
}
}

// resolvePaths resolves relative paths of certs/keys/chart and replace them with a absolute paths
func resolvePaths(relativeToFile string, s *state) {
dir := filepath.Dir(relativeToFile)

Expand Down

0 comments on commit 42c3c5d

Please sign in to comment.