Skip to content

Commit

Permalink
feat: switch to the production API urls by default
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The default API and Auth endpoints now point to the
production environment by default.
  • Loading branch information
Enda Phelan committed Apr 20, 2021
1 parent bee97a3 commit 0256779
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 53 deletions.
11 changes: 11 additions & 0 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ var (
TermsReviewSiteCode = "ocm"
)

// Auth Build variables
var (
ProductionAPIURL = "https://api.openshift.com"
StagingAPIURL = "https://api.stage.openshift.com"
DefaultClientID = "rhoas-cli-prod"
DefaultOfflineTokenClientID = "cloud-services"
ProductionAuthURL = "https://sso.redhat.com/auth/realms/redhat-external"
ProductionMasAuthURL = "https://identity.api.openshift.com/auth/realms/rhoas"
StagingMasAuthURL = "https://identity.api.stage.openshift.com/auth/realms/rhoas"
)

func init() {
if isDevBuild() {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"

"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/cmd/debug"
"github.com/redhat-developer/app-services-cli/pkg/connection"
Expand Down Expand Up @@ -78,12 +79,12 @@ func New(cliVersion string) *Factory {
builder.WithURL(cfg.APIUrl)
}
if cfg.AuthURL == "" {
cfg.AuthURL = connection.DefaultAuthURL
cfg.AuthURL = build.ProductionAuthURL
}
builder.WithAuthURL(cfg.AuthURL)

if cfg.MasAuthURL == "" {
cfg.MasAuthURL = connection.DefaultMasAuthURL
cfg.MasAuthURL = build.ProductionMasAuthURL
}
builder.WithMASAuthURL(cfg.MasAuthURL)

Expand Down
108 changes: 69 additions & 39 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,37 @@ import (
"github.com/spf13/cobra"
)

const (
devURL = "http://localhost:8000"
productionURL = "https://api.openshift.com"
stagingURL = "https://api.stage.openshift.com"
integrationURL = "https://api-integration.6943.hive-integration.openshiftapps.com"
defaultClientID = "rhoas-cli-prod"
defaultOfflineTokenClientID = "cloud-services"
)
// When the value of the `--api-gateway` option is one of the keys of this map it will be replaced by the
// corresponding value.
var apiGatewayAliases = map[string]string{
"production": build.ProductionAPIURL,
"prod": build.ProductionAPIURL,
"prd": build.ProductionAPIURL,
"staging": build.StagingAPIURL,
"stage": build.StagingAPIURL,
"stg": build.StagingAPIURL,
}

// When the value of the `--auth-url` option is one of the keys of this map it will be replaced by the
// corresponding value.
var authURLAliases = map[string]string{
"production": build.ProductionAuthURL,
"prod": build.ProductionAuthURL,
"prd": build.ProductionAuthURL,
"staging": build.ProductionAuthURL,
"stage": build.ProductionAuthURL,
"stg": build.ProductionAuthURL,
}

// When the value of the `--url` option is one of the keys of this map it will be replaced by the
// When the value of the `--mas-auth-url` option is one of the keys of this map it will be replaced by the
// corresponding value.
var urlAliases = map[string]string{
"production": productionURL,
"prod": productionURL,
"prd": productionURL,
"staging": stagingURL,
"stage": stagingURL,
"stg": stagingURL,
"integration": integrationURL,
"int": integrationURL,
"dev": devURL,
"development": devURL,
var masAuthURLAliases = map[string]string{
"production": build.ProductionMasAuthURL,
"prod": build.ProductionMasAuthURL,
"prd": build.ProductionMasAuthURL,
"staging": build.StagingMasAuthURL,
"stage": build.StagingMasAuthURL,
"stg": build.StagingMasAuthURL,
}

type Options struct {
Expand Down Expand Up @@ -83,18 +92,18 @@ func NewLoginCmd(f *factory.Factory) *cobra.Command {
Example: localizer.MustLocalizeFromID("login.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if opts.offlineToken != "" && opts.clientID == defaultClientID {
opts.clientID = defaultOfflineTokenClientID
if opts.offlineToken != "" && opts.clientID == build.DefaultClientID {
opts.clientID = build.DefaultOfflineTokenClientID
}
return runLogin(opts)
},
}

cmd.Flags().StringVar(&opts.url, "api-gateway", productionURL, localizer.MustLocalizeFromID("login.flag.apiGateway"))
cmd.Flags().StringVar(&opts.url, "api-gateway", build.ProductionAPIURL, localizer.MustLocalizeFromID("login.flag.apiGateway"))
cmd.Flags().BoolVar(&opts.insecureSkipTLSVerify, "insecure", false, localizer.MustLocalizeFromID("login.flag.insecure"))
cmd.Flags().StringVar(&opts.clientID, "client-id", defaultClientID, localizer.MustLocalizeFromID("login.flag.clientId"))
cmd.Flags().StringVar(&opts.authURL, "auth-url", connection.DefaultAuthURL, localizer.MustLocalizeFromID("login.flag.authUrl"))
cmd.Flags().StringVar(&opts.masAuthURL, "mas-auth-url", connection.DefaultMasAuthURL, localizer.MustLocalizeFromID("login.flag.masAuthUrl"))
cmd.Flags().StringVar(&opts.clientID, "client-id", build.DefaultClientID, localizer.MustLocalizeFromID("login.flag.clientId"))
cmd.Flags().StringVar(&opts.authURL, "auth-url", build.ProductionAuthURL, localizer.MustLocalizeFromID("login.flag.authUrl"))
cmd.Flags().StringVar(&opts.masAuthURL, "mas-auth-url", build.ProductionMasAuthURL, localizer.MustLocalizeFromID("login.flag.masAuthUrl"))
cmd.Flags().BoolVar(&opts.printURL, "print-sso-url", false, localizer.MustLocalizeFromID("login.flag.printSsoUrl"))
cmd.Flags().StringArrayVar(&opts.scopes, "scope", connection.DefaultScopes, localizer.MustLocalizeFromID("login.flag.scope"))
cmd.Flags().StringVarP(&opts.offlineToken, "token", "t", "", localizer.MustLocalizeFromID("login.flag.token"))
Expand All @@ -109,25 +118,22 @@ func runLogin(opts *Options) (err error) {
return err
}

// If the value of the `--url` is any of the aliases then replace it with the corresponding
// real URL:
unparsedGatewayURL, ok := urlAliases[opts.url]
if !ok {
unparsedGatewayURL = opts.url
gatewayURL, err := getURLFromAlias(opts.url, apiGatewayAliases)
if err != nil {
return err
}

gatewayURL, err := url.ParseRequestURI(unparsedGatewayURL)
authURL, err := getURLFromAlias(opts.authURL, authURLAliases)
if err != nil {
return err
}
if gatewayURL.Scheme != "http" && gatewayURL.Scheme != "https" {
return fmt.Errorf(localizer.MustLocalize(&localizer.Config{
MessageID: "login.error.schemeMissingFromUrl",
TemplateData: map[string]interface{}{
"URL": gatewayURL.String(),
},
}))
opts.authURL = authURL.String()

masAuthURL, err := getURLFromAlias(opts.masAuthURL, masAuthURLAliases)
if err != nil {
return err
}
opts.masAuthURL = masAuthURL.String()

if opts.offlineToken == "" {
tr := createTransport(opts.insecureSkipTLSVerify)
Expand Down Expand Up @@ -236,3 +242,27 @@ func createTransport(insecure bool) *http.Transport {
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
}

func getURLFromAlias(urlOrAlias string, urlAliasMap map[string]string) (*url.URL, error) {
// If the URL value is any of the aliases then replace it with the corresponding
// real URL:
unparsedGatewayURL, ok := urlAliasMap[urlOrAlias]
if !ok {
unparsedGatewayURL = urlOrAlias
}

gatewayURL, err := url.ParseRequestURI(unparsedGatewayURL)
if err != nil {
return nil, err
}
if gatewayURL.Scheme != "http" && gatewayURL.Scheme != "https" {
return nil, fmt.Errorf(localizer.MustLocalize(&localizer.Config{
MessageID: "login.error.schemeMissingFromUrl",
TemplateData: map[string]interface{}{
"URL": gatewayURL.String(),
},
}))
}

return gatewayURL, nil
}
3 changes: 2 additions & 1 deletion pkg/connection/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package connection

import (
"github.com/redhat-developer/app-services-cli/internal/build"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (b *Builder) BuildContext(ctx context.Context) (connection *KeycloakConnect
// Set the default URL, if needed:
rawAPIURL := b.apiURL
if rawAPIURL == "" {
rawAPIURL = DefaultURL
rawAPIURL = build.ProductionAPIURL
}
apiURL, err := url.Parse(rawAPIURL)
if err != nil {
Expand Down
11 changes: 0 additions & 11 deletions pkg/connection/keycloak_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/auth/token"
)

// Default values:
const (
DefaultClientID = "rhoas-cli-prod"
DefaultURL = "https://api.openshift.com"

// SSO defaults
DefaultAuthURL = "https://sso.redhat.com/auth/realms/redhat-external"
// MAS SSO defaults
DefaultMasAuthURL = "https://identity.api.openshift.com/auth/realms/rhoas"
)

var DefaultScopes = []string{
"openid",
}
Expand Down

0 comments on commit 0256779

Please sign in to comment.