Skip to content

Commit

Permalink
add --config option, remove --hybrid-config
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Apr 8, 2020
1 parent 3eae3fb commit ec5efcb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ dist/
bin/goreleaser
apigee-remote-service-cli
test.yaml
config.yaml
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ Then, run provision to get your configuration:

apigee-remote-service-cli provision --organization $ORG --environment $ENV --developer-email $EMAIL --runtime $RUNTIME --token $TOKEN

_Tip_
The CLI can parse the overrides.yaml file from your Hybrid setup to avoid
having to specify your organization, environment, and runtime.

apigee-remote-service-cli provision --hybrid-config $OVERRIDES --developer-email $EMAIL --token $TOKEN

### Apigee OPDK

If you are running Apigee Private Cloud (OPDK), you'll need to specify
Expand Down
11 changes: 8 additions & 3 deletions cmd/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const ( // modern
quotasURLFormat = "%s/quotas" // RemoteServiceProxyURL
rotateURLFormat = "%s/rotate" // RemoteServiceProxyURL

remoteServiceAPIURLFormat = "https://apigee-runtime-%s-%s:8443/remote-service" // org, env
remoteServiceAPIURLFormat = "https://apigee-runtime-%s-%s.%s:8443/remote-service" // org, env, namespace

fluentdConfigFile = "/opt/apigee/customer/default.properties"
)
Expand Down Expand Up @@ -687,10 +687,15 @@ func (p *provision) printConfig(cred *credential, printf shared.FormatFn, verify

if p.IsGCPManaged {
config.Tenant.ManagementAPI = ""
config.Tenant.RemoteServiceAPI = fmt.Sprintf(remoteServiceAPIURLFormat, p.Org, p.Env)
config.Tenant.FluentdConfigFile = fluentdConfigFile
config.Tenant.AllowUnverifiedSSLCert = true
config.Analytics.CollectionInterval = 10 * time.Second

// // Inside a mesh, we could use an internal address...
// // BUT it would have to be the same mesh. So, removing for now.
// if p.namespace != "" {
// config.Tenant.RemoteServiceAPI = fmt.Sprintf(remoteServiceAPIURLFormat, p.Org, p.Env, p.namespace)
// config.Tenant.AllowUnverifiedSSLCert = true
// }
}

if p.IsOPDK {
Expand Down
21 changes: 12 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,31 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
var addCommand = func(cmds ...*cobra.Command) {
for _, subC := range cmds {
subC.PersistentFlags().StringVarP(&rootArgs.RuntimeBase, "runtime", "r",
"", "Apigee runtime base URL (required for hybrid or OPDK)")
"", "Apigee runtime base URL (required for hybrid or opdk)")
subC.PersistentFlags().StringVarP(&rootArgs.ManagementBase, "management", "m",
shared.DefaultManagementBase, "Apigee management base URL")
subC.PersistentFlags().BoolVarP(&rootArgs.Verbose, "verbose", "v",
false, "verbose output")

subC.PersistentFlags().BoolVarP(&rootArgs.IsLegacySaaS, "legacy", "",
false, "Apigee SaaS (sets management URL)")
subC.PersistentFlags().BoolVarP(&rootArgs.IsOPDK, "opdk", "",
false, "Apigee OPDK")
false, "Apigee opdk")

subC.PersistentFlags().StringVarP(&rootArgs.Org, "organization", "o",
"", "Apigee organization name")
subC.PersistentFlags().StringVarP(&rootArgs.Env, "environment", "e",
"", "Apigee environment name")

subC.PersistentFlags().StringVarP(&rootArgs.Username, "username", "u",
"", "Apigee username (legacy or OPDK)")
"", "Apigee username (legacy or OPDK only)")
subC.PersistentFlags().StringVarP(&rootArgs.Password, "password", "p",
"", "Apigee password (legacy or OPDK)")
"", "Apigee password (legacy or OPDK only)")
subC.PersistentFlags().StringVarP(&rootArgs.Token, "token", "t",
"", "Apigee OAuth or SAML token (hybrid)")
"", "Apigee OAuth or SAML token (hybrid only)")

subC.PersistentFlags().StringVarP(&rootArgs.HybridConfigFile, "hybrid-config", "",
"", "Apigee Hybrid config file")
subC.PersistentFlags().StringVarP(&rootArgs.ConfigPath, "config", "c",
"", "Path to Apigee Remote Service config file")

c.AddCommand(subC)
}
Expand Down Expand Up @@ -125,10 +127,11 @@ func version(rootArgs *shared.RootArgs, printf, fatalf shared.FormatFn) *cobra.C
},
}

subC.PersistentFlags().StringVarP(&rootArgs.ConfigPath, "config", "c",
"", "Path to Apigee Remote Service config file")

subC.PersistentFlags().StringVarP(&rootArgs.RuntimeBase, "runtime", "r",
"", "Apigee runtime base URL")
subC.PersistentFlags().StringVarP(&rootArgs.HybridConfigFile, "hybrid-config", "",
"", "Apigee Hybrid config file")

return subC
}
Expand Down
20 changes: 18 additions & 2 deletions cmd/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ func cmdRotateCert(t *token, printf, fatalf shared.FormatFn) *cobra.Command {
Args: cobra.NoArgs,

Run: func(cmd *cobra.Command, _ []string) {
if t.ServerConfig != nil {
t.clientID = t.ServerConfig.Tenant.Key
t.clientSecret = t.ServerConfig.Tenant.Secret
}

missingFlagNames := []string{}
if t.clientID == "" {
missingFlagNames = append(missingFlagNames, "key")
}
if t.clientSecret == "" {
missingFlagNames = append(missingFlagNames, "secret")
}
if err := t.PrintMissingFlags(missingFlagNames); err != nil {
fatalf(err.Error())
}

t.rotateCert(printf, fatalf)
},
}
Expand All @@ -134,8 +150,8 @@ func cmdRotateCert(t *token, printf, fatalf shared.FormatFn) *cobra.Command {
c.Flags().StringVarP(&t.clientID, "key", "k", "", "provision key")
c.Flags().StringVarP(&t.clientSecret, "secret", "s", "", "provision secret")

c.MarkFlagRequired("key")
c.MarkFlagRequired("secret")
// c.MarkFlagRequired("key")
// c.MarkFlagRequired("secret")

return c
}
Expand Down
81 changes: 43 additions & 38 deletions shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/apigee/apigee-remote-service-cli/apigee"
"github.com/apigee/apigee-remote-service-envoy/server"
"gopkg.in/yaml.v3"
)

Expand All @@ -41,7 +42,8 @@ const (

internalProxyURLFormat = "%s://istioservices.%s/edgemicro" // runtime scheme, runtime domain (legacy SaaS and OPDK)
internalProxyURLFormatOPDK = "%s/edgemicro" // runtimeBase
remoteServiceProxyURLFormat = "%s/remote-service" // runtimeBase
remoteServicePath = "/remote-service"
remoteServiceProxyURLFormat = "%s" + remoteServicePath // runtimeBase
)

// BuildInfoType holds version information
Expand All @@ -56,19 +58,20 @@ var BuildInfo BuildInfoType

// RootArgs is the base struct to hold all command arguments
type RootArgs struct {
RuntimeBase string // "https://org-env.apigee.net"
ManagementBase string // "https://api.enterprise.apigee.com"
Verbose bool
Org string
Env string
Username string
Password string
Token string
NetrcPath string
IsOPDK bool
IsLegacySaaS bool
IsGCPManaged bool
HybridConfigFile string
RuntimeBase string // "https://org-env.apigee.net"
ManagementBase string // "https://api.enterprise.apigee.com"
Verbose bool
Org string
Env string
Username string
Password string
Token string
NetrcPath string
IsOPDK bool
IsLegacySaaS bool
IsGCPManaged bool
ConfigPath string
ServerConfig *server.Config // config loaded from ConfigPath

// the following is derived in Resolve()
InternalProxyURL string
Expand All @@ -79,18 +82,14 @@ type RootArgs struct {

// Resolve is used to populate shared args, it's automatically called prior when creating the root command
func (r *RootArgs) Resolve(skipAuth, requireRuntime bool) error {

r.loadConfig()

if r.IsLegacySaaS && r.IsOPDK {
return errors.New("--legacy and --opdk options are exclusive")
}
r.IsGCPManaged = !(r.IsLegacySaaS || r.IsOPDK)

if r.IsGCPManaged {
err := r.loadOverrideConfig()
if err != nil {
return fmt.Errorf("--hybrid-config %s load failed: %s", r.HybridConfigFile, err)
}
}

if r.IsLegacySaaS && r.ManagementBase == DefaultManagementBase {
r.ManagementBase = LegacySaaSManagementBase
}
Expand Down Expand Up @@ -192,34 +191,33 @@ type OverrideEnv struct {
HostAlias string `yaml:"hostAlias"`
}

func (r *RootArgs) loadOverrideConfig() error {
if r.HybridConfigFile == "" {
func (r *RootArgs) loadConfig() error {
if r.ConfigPath == "" {
return nil
}
c := &overrideConfig{}
yamlFile, err := ioutil.ReadFile(r.HybridConfigFile)
c := &server.Config{}
yamlFile, err := ioutil.ReadFile(r.ConfigPath)
if err == nil {
err = yaml.Unmarshal(yamlFile, c)
}
if err != nil {
return err
}

if r.Org == "" {
r.Org = c.Org
}
r.ServerConfig = c
r.ManagementBase = c.Tenant.ManagementAPI
r.RuntimeBase = strings.Split(c.Tenant.RemoteServiceAPI, remoteServicePath)[0]

if len(c.Envs) == 1 {
loadEnv(r, c.Envs[0])
} else {
for _, e := range c.Envs {
loadEnv(r, e)
break
}
}
r.Org = c.Tenant.OrgName
r.Env = c.Tenant.EnvName

if r.Env == "" {
return fmt.Errorf("--hybrid-config %s has multiple envs, --env required", r.HybridConfigFile)
switch r.ManagementBase {
case LegacySaaSManagementBase:
r.IsLegacySaaS = true
case GCPExperienceBase:
r.IsGCPManaged = true
default:
r.IsOPDK = true
}

return nil
Expand All @@ -233,3 +231,10 @@ func loadEnv(r *RootArgs, env OverrideEnv) {
r.RuntimeBase = fmt.Sprintf("https://%s", env.HostAlias)
}
}

func (r *RootArgs) PrintMissingFlags(missingFlagNames []string) error {
if len(missingFlagNames) > 0 {
return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))
}
return nil
}

0 comments on commit ec5efcb

Please sign in to comment.