Skip to content

Commit

Permalink
remove email validation and fix b64 of secret (#604)
Browse files Browse the repository at this point in the history
* remove email validation and fix b64 of secret

* modifies auth flow for k3d default
  • Loading branch information
jarededwards authored Oct 23, 2022
1 parent c9f9ade commit 1e3a982
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
33 changes: 17 additions & 16 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,27 @@ validated and configured.`,
return err
}

// todo: wire it / check if gitlab or github install
if config.GitHubPersonalAccessToken == "" && !globalFlags.SilentMode {
if viper.GetString("cloud") == flagset.CloudK3d {
if config.GitHubPersonalAccessToken == "" && !globalFlags.SilentMode {

httpClient := http.DefaultClient
gitHubService := services.NewGitHubService(httpClient)
gitHubHandler := handlers.NewGitHubHandler(gitHubService)
gitHubAccessToken, err := gitHubHandler.AuthenticateUser()
if err != nil {
return err
}
httpClient := http.DefaultClient
gitHubService := services.NewGitHubService(httpClient)
gitHubHandler := handlers.NewGitHubHandler(gitHubService)
gitHubAccessToken, err := gitHubHandler.AuthenticateUser()
if err != nil {
return err
}

if len(gitHubAccessToken) == 0 {
return errors.New("unable to retrieve a GitHub token for the user")
}
if len(gitHubAccessToken) == 0 {
return errors.New("unable to retrieve a GitHub token for the user")
}

// todo: set common way to load env. values (viper->struct->load-env)
if err := os.Setenv("GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil {
return err
// todo: set common way to load env. values (viper->struct->load-env)
if err := os.Setenv("GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil {
return err
}
log.Println("\nGITHUB_AUTH_TOKEN set via OAuth")
}
log.Println("\nGITHUB_AUTH_TOKEN set via OAuth")
}

if globalFlags.SilentMode {
Expand Down
25 changes: 13 additions & 12 deletions internal/flagset/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flagset

import (
"errors"
"fmt"
"log"

"github.com/kubefirst/kubefirst/configs"
Expand Down Expand Up @@ -34,7 +33,7 @@ func DefineInstallerGenericFlags(currentCommand *cobra.Command) {
// Generic Installer flags:
currentCommand.Flags().String("cluster-name", "kubefirst", "the cluster name, used to identify resources on cloud provider")
currentCommand.Flags().String("admin-email", "", "the email address for the administrator as well as for lets-encrypt certificate emails")
currentCommand.Flags().String("cloud", "", "the cloud to provision infrastructure in")
currentCommand.Flags().String("cloud", "k3d", "the cloud to provision infrastructure in")
currentCommand.Flags().String("gitops-owner", "kubefirst", "git owner of gitops, this may be a user or a org to support forks for testing")
currentCommand.Flags().String("gitops-repo", "gitops", "version/branch used on git clone")
currentCommand.Flags().String("gitops-branch", "", "version/branch used on git clone - former: version-gitops flag")
Expand Down Expand Up @@ -209,20 +208,22 @@ func experimentalModeTweaks(flags InstallerGenericFlags) InstallerGenericFlags {
func validateInstallationFlags() error {
//If you are changind this rules, please ensure to update:
// internal/flagset/init_test.go
if len(viper.GetString("adminemail")) < 1 {
message := "missing flag --admin-email"
log.Println(message)
return errors.New(message)
}
// todo validate on email address if not local
// if len(viper.GetString("adminemail")) < 1 {
// message := "missing flag --admin-email"
// log.Println(message)
// return errors.New(message)
// }
if len(viper.GetString("cloud")) < 1 {
message := "missing flag --cloud, supported values: " + CloudAws + ", " + CloudK3d
log.Println(message)
return errors.New(message)
}
if viper.GetString("cloud") == CloudLocal && !viper.GetBool("github.enabled") {
message := fmt.Sprintf(" flag --cloud %s is not supported for non-github installations. Please, provide the flags '--github-user ghuser --github-org ghorg' to be able to use local install ", CloudK3d)
log.Println(message)
return errors.New(message)
}
// todo double check this validation with the new default of k3d
// if viper.GetString("cloud") == CloudLocal && !viper.GetBool("github.enabled") {
// message := fmt.Sprintf(" flag --cloud %s is not supported for non-github installations. Please, provide the flags '--github-user ghuser --github-org ghorg' to be able to use local install ", CloudK3d)
// log.Println(message)
// return errors.New(message)
// }
return nil
}
37 changes: 37 additions & 0 deletions internal/handlers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ func (handler GitHubHandler) AuthenticateUser() (string, error) {
}

if len(gitHubAccessToken) > 0 {
githubOwner := getGithubOwner(gitHubAccessToken)

fmt.Printf("\n\nGitHub token set!\n\n")
viper.Set("github.token", gitHubAccessToken)
viper.Set("github.owner", githubOwner)
viper.WriteConfig()
return gitHubAccessToken, nil
}
Expand All @@ -106,3 +109,37 @@ func (handler GitHubHandler) AuthenticateUser() (string, error) {
}
return gitHubAccessToken, nil
}

func getGithubOwner(gitHubAccessToken string) string {

req, err := http.NewRequest(http.MethodGet, "https://api.github.com/user", nil)
if err != nil {
log.Println("error setting request")
}
req.Header.Add("Content-Type", pkg.JSONContentType)
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", gitHubAccessToken))

res, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("error doing request")
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println("error unmarshalling request")
}
type GitHubUser struct {
Login string `json:"login"`
}

var githubUser GitHubUser
err = json.Unmarshal(body, &githubUser)
if err != nil {
log.Println(err)
}
log.Println(githubUser.Login)
return githubUser.Login

}
6 changes: 5 additions & 1 deletion internal/k3d/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k3d

import (
"context"
"encoding/base64"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -64,7 +65,10 @@ func AddK3DSecrets(dryrun bool) error {
viper.Set("kubernetes.argo-ci.secret.created", true)
viper.WriteConfig()

dockerConfigString := fmt.Sprintf(`{"auths": {"https://ghcr.io/": {"auth": "%s:%s"}}}`, viper.GetString("github.user"), os.Getenv("GITHUB_AUTH_TOKEN"))
usernamePasswordString := fmt.Sprintf("%s:%s", viper.GetString("github.user"), os.Getenv("GITHUB_AUTH_TOKEN"))
usernamePasswordStringB64 := base64.StdEncoding.EncodeToString([]byte(usernamePasswordString))

dockerConfigString := fmt.Sprintf(`{"auths": {"https://ghcr.io/": {"auth": "%s"}}}`, usernamePasswordStringB64)
argoDockerSecrets := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "docker-config", Namespace: "argo"},
Data: map[string][]byte{"config.json": []byte(dockerConfigString)},
Expand Down

0 comments on commit 1e3a982

Please sign in to comment.