Skip to content

Commit

Permalink
Add new registry auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Jul 3, 2018
1 parent 1d21247 commit 9e59e25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
29 changes: 16 additions & 13 deletions auth/auth.go
Expand Up @@ -32,7 +32,8 @@ func basicAuth(username string) string {

// oAuth handles oAuth with houston api
func oAuth(oAuthUrl string) string {
fmt.Sprintf(messages.HOUSTON_OAUTH_REDIRECT, oAuthUrl)
fmt.Println("\n" + messages.HOUSTON_OAUTH_REDIRECT)
fmt.Println(oAuthUrl + "\n")
authSecret := input.InputText(messages.INPUT_OAUTH_TOKEN)

token, err := API.CreateOAuthToken(authSecret)
Expand All @@ -44,12 +45,12 @@ func oAuth(oAuthUrl string) string {
}

// registryAuth authenticates with the private registry
func registryAuth(username, password string) error {
registry := config.CFG.RegistryAuthority.GetString()
func registryAuth() error {
registry := config.RegistryUrl()
token := config.CFG.CloudAPIToken.GetProjectString()

dockerErr := docker.ExecLogin(registry, username, password)
dockerErr := docker.ExecLogin(registry, "user", token)
if dockerErr != nil {
// Println instead of panic to prevent excessive error logging to stdout on a failed login
fmt.Println(dockerErr)
os.Exit(1)
}
Expand All @@ -59,16 +60,20 @@ func registryAuth(username, password string) error {
return nil
}

// Login logs a user into the docker registry. Will need to login to Houston next.
func Login() {
// Login handles authentication to houston and registry
func Login(oAuthOnly bool) {
token := ""
authConfig, err := API.GetAuthConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

username := input.InputText(messages.INPUT_USERNAME)
username := ""
if !oAuthOnly {
username = input.InputText(messages.INPUT_USERNAME)
}

if len(username) == 0 {
if authConfig.GoogleEnabled {
token = oAuth(authConfig.OauthUrl)
Expand All @@ -85,13 +90,11 @@ func Login() {
}

config.CFG.CloudAPIToken.SetProjectString(token)

// pass successful credentials to config
// TODO
// config.CFG.RegistryAuth.SetProjectString(config.EncodeAuth(username, password))
registryAuth()
}

// Logout logs a user out of the docker registry. Will need to logout of Houston next.
func Logout() {
docker.Exec("logout", config.CFG.RegistryAuthority.GetString())
// forget jwt
config.CFG.CloudAPIToken.SetProjectString("")
}
15 changes: 4 additions & 11 deletions cmd/auth.go
Expand Up @@ -3,13 +3,12 @@ package cmd
import (
"github.com/astronomerio/astro-cli/auth"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/messages"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var (
domainOverride string
oAuthOnly bool

authRootCmd = &cobra.Command{
Use: "auth",
Expand Down Expand Up @@ -39,6 +38,7 @@ func init() {
// Auth login
authRootCmd.AddCommand(authLoginCmd)
authLoginCmd.Flags().StringVarP(&domainOverride, "domain", "d", "", "pass the cluster domain for authentication")
authLoginCmd.Flags().BoolVarP(&oAuthOnly, "oauth", "o", false, "do not prompt for local auth")
// Auth logout
authRootCmd.AddCommand(authLogoutCmd)
}
Expand All @@ -48,17 +48,10 @@ func authLogin(cmd *cobra.Command, args []string) error {
config.CFG.CloudDomain.SetProjectString(domainOverride)
}

projectCloudDomain := config.CFG.CloudDomain.GetProjectString()
globalCloudDomain := config.CFG.CloudDomain.GetHomeString()

if len(projectCloudDomain) == 0 && len(globalCloudDomain) == 0 {
return errors.New(messages.CONFIG_DOMAIN_NOT_SET_ERROR)
}

auth.Login()
auth.Login(oAuthOnly)
return nil
}

func authLogout(cmd *cobra.Command, args []string) {
auth.Logout()
// auth.Logout()
}
2 changes: 1 addition & 1 deletion houston/houston.go
Expand Up @@ -142,7 +142,7 @@ func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {
// }

var response httputil.HTTPResponse
httpResponse, err := c.HTTPClient.Do("POST", config.APIURL(), &doOpts)
httpResponse, err := c.HTTPClient.Do("POST", config.APIUrl(), &doOpts)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion messages/messages.go
Expand Up @@ -50,7 +50,7 @@ var (
HOUSTON_DEPLOYING_PROMPT = "Deploying: %s\n"
HOUSTON_NO_DEPLOYMENTS_ERROR = "No airflow deployments found"
HOUSTON_SELECT_DEPLOYMENT_PROMT = "Select which airflow deployment you want to deploy to:"
HOUSTON_OAUTH_REDIRECT = "Please visit the following URL, authenticate and paste token in next prompt \n%s"
HOUSTON_OAUTH_REDIRECT = "Please visit the following URL, authenticate and paste token in next prompt "
HOUSTON_OAUTH_DISABLED = "OAuth is disabled, contact administrator or defer to basic auth"
HOUSTON_INVALID_DEPLOYMENT_KEY = "Invalid deployment selection"

Expand Down

0 comments on commit 9e59e25

Please sign in to comment.