forked from Nordstrom/kubelogin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RE: Nordstrom#45 - Added version to cli and server packages - Version is injected by make based either on a Env VAR or autoincremented patch level - If version is not injected during build we panic - For testing a simple string is added - Added unit tests - Very minor clean up and style improvment
- Loading branch information
Showing
7 changed files
with
298 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
type app struct { | ||
filenameWithPath string | ||
kubectlUser string | ||
kubeloginAlias string | ||
kubeloginServer string | ||
} | ||
|
||
func (app *app) makeExchange(token string) error { | ||
url := fmt.Sprintf("%s/exchange?token=%s", app.kubeloginServer, token) | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
log.Printf("Unable to create request. %s", err) | ||
return err | ||
} | ||
client := http.DefaultClient | ||
res, err := client.Do(req) | ||
if err != nil { | ||
log.Printf("Unable to make request. %s", err) | ||
return err | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
log.Fatalf("Failed to retrieve token from kubelogin server. Please try again or contact your administrator") | ||
} | ||
defer res.Body.Close() // nolint: errcheck | ||
jwt, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
log.Printf("Unable to read response body. %s", err) | ||
return err | ||
} | ||
if err := app.configureKubectl(string(jwt)); err != nil { | ||
log.Printf("Error when setting credentials: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (app *app) tokenHandler(w http.ResponseWriter, r *http.Request) { | ||
token := r.FormValue("token") | ||
if err := app.makeExchange(token); err != nil { | ||
log.Fatalf("Could not exchange token for jwt %v", err) | ||
} | ||
fmt.Fprint(w, "You are now logged in! You can close this window") | ||
doneChannel <- true | ||
} | ||
|
||
func (app *app) configureKubectl(jwt string) error { | ||
configCmd := exec.Command("kubectl", "config", "set-credentials", app.kubectlUser, "--token="+jwt) | ||
return configCmd.Run() | ||
} | ||
|
||
func (app *app) generateAuthURL() (string, string, error) { | ||
portNum, err := findFreePort() | ||
if err != nil { | ||
log.Print("err, could not find an open port") | ||
return "", "", err | ||
} | ||
|
||
loginURL := fmt.Sprintf("%s/login?port=%s", app.kubeloginServer, portNum) | ||
|
||
return loginURL, portNum, nil | ||
} | ||
|
||
func (app *app) getConfigSettings(alias string) error { | ||
yamlFile, err := ioutil.ReadFile(app.filenameWithPath) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read config file for login use") | ||
} | ||
var config Config | ||
if err := yaml.Unmarshal(yamlFile, &config); err != nil { | ||
return errors.Wrap(err, "failed to unmarshal yaml file for login use") | ||
} | ||
|
||
aliasConfig, ok := config.aliasSearch(alias) | ||
if !ok { | ||
return errors.New("Could not find specified alias, check spelling or use the config verb to create an alias") | ||
} | ||
app.kubectlUser = aliasConfig.KubectlUser | ||
app.kubeloginServer = aliasConfig.BaseURL | ||
return nil | ||
} | ||
|
||
func (app *app) configureFile(kubeloginrcAlias string, loginServerURL *url.URL, kubectlUser string) error { | ||
var config Config | ||
aliasConfig := config.newAliasConfig(kubeloginrcAlias, loginServerURL.String(), kubectlUser) | ||
yamlFile, err := ioutil.ReadFile(app.filenameWithPath) | ||
if err != nil { | ||
return config.createConfig(app.filenameWithPath, aliasConfig) // Either error or nil value | ||
} | ||
if err := yaml.Unmarshal(yamlFile, &config); err != nil { | ||
return errors.Wrap(err, "failed to unmarshal yaml file") | ||
} | ||
foundAliasConfig, ok := config.aliasSearch(aliasFlag) | ||
if !ok { | ||
newConfig := config.newAliasConfig(kubeloginrcAlias, loginServerURL.String(), kubectlUser) | ||
config.appendAlias(newConfig) | ||
if err := config.writeToFile(app.filenameWithPath); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Print("New Alias configured") | ||
return nil | ||
} | ||
|
||
return config.updateAlias(foundAliasConfig, loginServerURL, app.filenameWithPath) // Either error or nil value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
// Config contains the array of aliases (AliasConfig) | ||
type Config struct { | ||
Aliases []*AliasConfig `yaml:"aliases"` | ||
} | ||
|
||
func (config *Config) aliasSearch(alias string) (*AliasConfig, bool) { | ||
for index, aliases := range config.Aliases { | ||
if alias == aliases.Alias { | ||
return config.Aliases[index], true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (config *Config) createConfig(onDiskFile string, aliasConfig AliasConfig) error { | ||
log.Print("Couldn't find config file in root directory. Creating config file...") | ||
_, e := os.Stat(onDiskFile) // Does config file exist? | ||
if os.IsNotExist(e) { // Create file | ||
fh, err := os.Create(onDiskFile) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create file in root directory") | ||
} | ||
_ = fh.Close() | ||
} | ||
|
||
log.Print("Config file created, setting config values...") | ||
config.Aliases = make([]*AliasConfig, 0) | ||
config.appendAlias(aliasConfig) | ||
if err := config.writeToFile(onDiskFile); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Print("File configured") | ||
return nil | ||
} | ||
|
||
func (config *Config) newAliasConfig(kubeloginrcAlias, loginServerURL, kubectlUser string) AliasConfig { | ||
newConfig := AliasConfig{ | ||
BaseURL: loginServerURL, | ||
Alias: kubeloginrcAlias, | ||
KubectlUser: kubectlUser, | ||
} | ||
return newConfig | ||
} | ||
|
||
func (config *Config) appendAlias(aliasConfig AliasConfig) { | ||
config.Aliases = append(config.Aliases, &aliasConfig) | ||
} | ||
|
||
func (config *Config) writeToFile(onDiskFile string) error { | ||
marshaledYaml, err := yaml.Marshal(config) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to marshal alias yaml") | ||
} | ||
if err := ioutil.WriteFile(onDiskFile, marshaledYaml, 0600); err != nil { | ||
return errors.Wrap(err, "failed to write to kubeloginrc file with the alias") | ||
} | ||
log.Printf(string(marshaledYaml)) | ||
return nil | ||
} | ||
|
||
func (config *Config) updateAlias(aliasConfig *AliasConfig, loginServerURL *url.URL, onDiskFile string) error { | ||
aliasConfig.KubectlUser = userFlag | ||
aliasConfig.BaseURL = loginServerURL.String() | ||
if err := config.writeToFile(onDiskFile); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Print("Alias updated") | ||
return nil | ||
} |
Oops, something went wrong.