Skip to content

Commit

Permalink
Merge pull request #337 from Chamindu36/chamindu_master_fixes
Browse files Browse the repository at this point in the history
Make --token flag optional when adding an environment
  • Loading branch information
malinthaprasan authored Jun 24, 2020
2 parents 76e0aba + 249d41c commit eab1fb5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 25 deletions.
26 changes: 16 additions & 10 deletions import-export-cli/cmd/addEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const addEnvCmdLiteral = "add-env"
const addEnvCmdShortDesc = "Add Environment to Config file"
const addEnvCmdLongDesc = "Add new environment and its related endpoints to the config file"
const addEnvCmdExamples = utils.ProjectName + ` ` + addEnvCmdLiteral + ` -e production \
--apim https://localhost:9443 \
--token https://localhost:8243/token
--apim https://localhost:9443
` + utils.ProjectName + ` ` + addEnvCmdLiteral + ` -e test \
--registration https://idp.com:9443 \
Expand All @@ -54,10 +53,10 @@ const addEnvCmdExamples = utils.ProjectName + ` ` + addEnvCmdLiteral + ` -e prod
--registration https://idp.com:9443 \
--token https://gw.com:8243/token
NOTE: The flags --environment (-e) and --token are mandatory
You can either provide only the 2 flags --apim and --token, or all the other 5 flags (--registration --publisher --devportal --admin --token) without providing --apim flag.
If you are omitting any of --registration --publisher --devportal --admin flags, you need to specify --apim flag with the API Manager endpoint.`

NOTE: The flag --environment (-e) is mandatory.
You can either provide only the flag --apim , or all the other 4 flags (--registration --publisher --devportal --admin --token) without providing --apim flag.
If you are omitting any of --registration --publisher --devportal --admin flags, you need to specify --apim flag with the API Manager endpoint. In both of the
cases --token flag is optional and use it to specify the gateway token endpoint. This will be used for "apictl get-keys" operation.`
// addEnvCmd represents the addEnv command
var addEnvCmd = &cobra.Command{
Use: addEnvCmdLiteral,
Expand Down Expand Up @@ -92,16 +91,24 @@ func executeAddEnvCmd(mainConfigFilePath string) {
// @param tokenEndpoint : Token Endpoint for the environment
// @param mainConfigFilePath : Path to file where env endpoints are stored
// @return error
var isDefaultTokenEndpointSet bool = false
func addEnv(envName string, envEndpoints *utils.EnvEndpoints, mainConfigFilePath string) error {

if envName == "" {
// name of the environment is blank
return errors.New("Name of the environment cannot be blank")
}

if envEndpoints.TokenEndpoint == "" {
// if mandatory token endpoint is blank
utils.ShowHelpCommandTip(addEnvCmdLiteral)
return errors.New("Token endpoint cannot be blank")
// If token endpoint string is empty,then assign the default value
if envEndpoints.ApiManagerEndpoint != "" && !isDefaultTokenEndpointSet {
isDefaultTokenEndpointSet = true
envEndpoints.TokenEndpoint = utils.GetTokenEndPointFromAPIMEndpoint(envEndpoints.ApiManagerEndpoint)
}
if envEndpoints.PublisherEndpoint != "" && !isDefaultTokenEndpointSet {
envEndpoints.TokenEndpoint = utils.GetTokenEndPointFromPublisherEndpoint(envEndpoints.PublisherEndpoint)
}
fmt.Printf("Default token endpoint '%s' is added as the token endpoint \n", envEndpoints.TokenEndpoint)
}

if envEndpoints.ApiManagerEndpoint == "" {
Expand Down Expand Up @@ -165,5 +172,4 @@ func init() {
"Registration endpoint for the environment")
addEnvCmd.Flags().StringVar(&flagAdminEndpoint, "admin", "", "Admin endpoint for the environment")
_ = addEnvCmd.MarkFlagRequired("environment")
_ = addEnvCmd.MarkFlagRequired("token")
}
32 changes: 26 additions & 6 deletions import-export-cli/cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ const genKeyCmdLiteral = "get-keys"
const genKeyCmdShortDesc = "Generate access token to invoke the API or API Product"
const genKeyCmdLongDesc = `Generate JWT token to invoke the API or API Product by subscribing to a default application for testing purposes`
const genKeyCmdExamples = utils.ProjectName + " " + genKeyCmdLiteral + ` -n TwitterAPI -v 1.0.0 -e dev --provider admin
NOTE: Both the flags (--name (-n) and --environment (-e)) are mandatory`

NOTE: Both the flags (--name (-n) and --environment (-e)) are mandatory.
You can override the default token endpoint using --token (-t) optional flag providing a new token endpoint`
var keyGenEnv string
var apiName string
var apiVersion string
var apiProvider string
var tokenType string
var subscriptionThrottlingTier string
var applicationThrottlingPolicy string
var keyGenTokenEndpoint string

var genKeyCmd = &cobra.Command{
Use: genKeyCmdLiteral,
Expand All @@ -61,6 +62,14 @@ var genKeyCmd = &cobra.Command{

//Subscribe the given API or API Product to the default application and generate an access token
func getKeys() {

//Override the value of token endpoint if it is provided with get-keys command
if keyGenTokenEndpoint != "" {
fmt.Printf("New token endpoint '%s' is used as the token endpoint \n", keyGenTokenEndpoint)
} else {
var defaultTokenEndpoint = utils.GetTokenEndpointOfEnv(keyGenEnv,utils.MainConfigFilePath)
fmt.Printf("'%s' is used as the token endpoint \n", defaultTokenEndpoint)
}
cred, err := getCredentials(keyGenEnv)
if err != nil {
utils.HandleErrorAndExit("Error getting credentials", err)
Expand Down Expand Up @@ -324,8 +333,14 @@ func generateAccessToken(credential credentials.Credential) (string, error) {
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBasicPrefix + " " + b64encodedCredentials
headers[utils.HeaderAccept] = utils.HeaderValueApplicationJSON

//Retrieving the token endpoint of the relevant environment
tokenEndpoint := utils.GetTokenEndpointOfEnv(keyGenEnv, utils.MainConfigFilePath)
var tokenEndpoint string
//Retrieving the token endpoint of the relevant environment if new token endpoint is not given
//If new token endpoint is given replace token endpoint of the relevant environment into new token endpoint for the instance
if keyGenTokenEndpoint == "" {
tokenEndpoint = utils.GetTokenEndpointOfEnv(keyGenEnv, utils.MainConfigFilePath)
} else {
tokenEndpoint = keyGenTokenEndpoint
}
//Prepping query params
body := "grant_type=password&username=" + credential.Username + "&password=" +
encodeURL.QueryEscape(credential.Password) + "&validity_period=" + string(utils.DefaultTokenValidityPeriod) +
Expand Down Expand Up @@ -729,8 +744,12 @@ func createApplication(accessToken string, throttlingPolicy string) (string, str
// @param scopes[] : Scopes to generate the token
// @return accessToken, error
func getNewToken(key *utils.ApplicationKey, scopes []string) (string, error) {
tokenEndpoint := utils.GetTokenEndpointOfEnv(keyGenEnv, utils.MainConfigFilePath)

var tokenEndpoint string
if keyGenTokenEndpoint == "" {
tokenEndpoint = utils.GetTokenEndpointOfEnv(keyGenEnv, utils.MainConfigFilePath)
} else {
tokenEndpoint = keyGenTokenEndpoint
}
body := "grant_type=client_credentials&scope=" + strings.Join(scopes, " ")

headers := make(map[string]string)
Expand Down Expand Up @@ -848,6 +867,7 @@ func init() {
genKeyCmd.Flags().StringVarP(&apiName, "name", "n", "", "API or API Product to generate keys")
genKeyCmd.Flags().StringVarP(&apiVersion, "version", "v", "", "Version of the API or API Product")
genKeyCmd.Flags().StringVarP(&apiProvider, "provider", "r", "", "Provider of the API or API Product")
genKeyCmd.Flags().StringVarP(&keyGenTokenEndpoint, "token", "t", "", "Token endpoint URL of Environment")
_ = genKeyCmd.MarkFlagRequired("name")
_ = genKeyCmd.MarkFlagRequired("environment")
}
2 changes: 1 addition & 1 deletion import-export-cli/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetDefaultCredentialStore() (Store, error) {

// GetOAuthAccessToken generates an accesstoken for CLI
func GetOAuthAccessToken(credential Credential, env string) (string, error) {
tokenEndpoint := utils.GetTokenEndpointOfEnv(env, utils.MainConfigFilePath)
tokenEndpoint := utils.GetInternalTokenEndpointOfEnv(env, utils.MainConfigFilePath)
data, err := utils.GetOAuthTokens(credential.Username, credential.Password,
Base64Encode(credential.ClientId+":"+credential.ClientSecret),
tokenEndpoint)
Expand Down
10 changes: 5 additions & 5 deletions import-export-cli/docs/apictl_add-env.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ apictl add-env [flags]

```
apictl add-env -e production \
--apim https://localhost:9443 \
--token https://localhost:8243/token
--apim https://localhost:9443
apictl add-env -e test \
--registration https://idp.com:9443 \
Expand All @@ -29,9 +28,10 @@ apictl add-env -e dev \
--registration https://idp.com:9443 \
--token https://gw.com:8243/token
NOTE: The flags --environment (-e) and --token are mandatory
You can either provide only the 2 flags --apim and --token, or all the other 5 flags (--registration --publisher --devportal --admin --token) without providing --apim flag.
If you are omitting any of --registration --publisher --devportal --admin flags, you need to specify --apim flag with the API Manager endpoint.
NOTE: The flag --environment (-e) is mandatory.
You can either provide only the flag --apim , or all the other 4 flags (--registration --publisher --devportal --admin --token) without providing --apim flag.
If you are omitting any of --registration --publisher --devportal --admin flags, you need to specify --apim flag with the API Manager endpoint. In both of the
cases --token flag is optional and use it to specify the gateway token endpoint. This will be used for "apictl get-keys" operation.
```

### Options
Expand Down
4 changes: 3 additions & 1 deletion import-export-cli/docs/apictl_get-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ apictl get-keys [flags]

```
apictl get-keys -n TwitterAPI -v 1.0.0 -e dev --provider admin
NOTE: Both the flags (--name (-n) and --environment (-e)) are mandatory
NOTE: Both the flags (--name (-n) and --environment (-e)) are mandatory.
You can override the default token endpoint using --token (-t) optional flag providing a new token endpoint
```

### Options
Expand All @@ -24,6 +25,7 @@ NOTE: Both the flags (--name (-n) and --environment (-e)) are mandatory
-h, --help help for get-keys
-n, --name string API or API Product to generate keys
-r, --provider string Provider of the API or API Product
-t, --token string Token endpoint URL of Environment
-v, --version string Version of the API or API Product
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ _apictl_add-env()
must_have_one_flag=()
must_have_one_flag+=("--environment=")
must_have_one_flag+=("-e")
must_have_one_flag+=("--token=")
must_have_one_noun=()
noun_aliases=()
}
Expand Down Expand Up @@ -879,6 +878,9 @@ _apictl_get-keys()
flags+=("--provider=")
two_word_flags+=("-r")
local_nonpersistent_flags+=("--provider=")
flags+=("--token=")
two_word_flags+=("-t")
local_nonpersistent_flags+=("--token=")
flags+=("--version=")
two_word_flags+=("-v")
local_nonpersistent_flags+=("--version=")
Expand Down
1 change: 1 addition & 0 deletions import-export-cli/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const defaultAdminApplicationListEndpointSuffix = "api/am/admin/v1/applications"
const defaultDevPortalApplicationListEndpointSuffix = "api/am/store/v1/applications"
const defaultDevPortalThrottlingPoliciesEndpointSuffix = "api/am/store/v1/throttling-policies"
const defaultClientRegistrationEndpointSuffix = "client-registration/v0.16/register"
const defaultTokenEndPoint = "oauth2/token"

const DefaultEnvironmentName = "default"

Expand Down
45 changes: 45 additions & 0 deletions import-export-cli/utils/envManagementUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package utils

import (
"errors"
"strings"
)

// EnvExistsInKeysFile
Expand Down Expand Up @@ -158,6 +159,12 @@ func GetApiManagerEndpointOfEnv(env, filePath string) string {
return envEndpoints.ApiManagerEndpoint
}

// Get PublisherEndpoint of a given environment
func GetPublisherEndpointOfEnv(env, filePath string) string {
envEndpoints, _ := GetEndpointsOfEnvironment(env, filePath)
return envEndpoints.PublisherEndpoint
}

// Get AdminEndpoint of a given environment
func GetAdminEndpointOfEnv(env, filePath string) string {
envEndpoints, _ := GetEndpointsOfEnvironment(env, filePath)
Expand Down Expand Up @@ -309,3 +316,41 @@ func GetDefaultEnvironment(mainConfigFilePath string) string {
}
return ""
}

//get default token endpoint given from an apim endpoint
func GetTokenEndPointFromAPIMEndpoint(apimEndpoint string) string {
if strings.HasSuffix(apimEndpoint,"/"){
return apimEndpoint + defaultTokenEndPoint
} else {
return apimEndpoint + "/" + defaultTokenEndPoint
}
}

//get default token endpoint given from a publisher endpoint
func GetTokenEndPointFromPublisherEndpoint (publisherEndpoint string) string {
if strings.Contains(publisherEndpoint,"publisher"){
trimmedString := strings.Split(publisherEndpoint,"publisher")
publisherEndpoint = trimmedString[0]
}

if strings.HasSuffix(publisherEndpoint,"/"){
return publisherEndpoint + defaultTokenEndPoint
} else {
return publisherEndpoint + "/" + defaultTokenEndPoint
}
}

// Get internalTokenEndpoint for REST api operations
// @return endpoint url derived from publisher or apim endpoint
func GetInternalTokenEndpointOfEnv(env, filePath string ) string {
var internalTokenEndpoint string
apiManagerEndpointOfEnv := GetApiManagerEndpointOfEnv(env, filePath)
if apiManagerEndpointOfEnv != "" {
internalTokenEndpoint = GetTokenEndPointFromAPIMEndpoint(apiManagerEndpointOfEnv)

} else {
publisherEndpointOfEnv := GetPublisherEndpointOfEnv(env,filePath)
internalTokenEndpoint = GetTokenEndPointFromPublisherEndpoint(publisherEndpointOfEnv)
}
return internalTokenEndpoint
}
2 changes: 1 addition & 1 deletion import-export-cli/utils/tokenManagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func ExecutePreCommandWithOAuth(environment, flagUsername, flagPassword, mainCon
envKeysAllFilePath string) (accessToken string, err error) {
if EnvExistsInMainConfigFile(environment, mainConfigFilePath) {
registrationEndpoint := GetRegistrationEndpointOfEnv(environment, mainConfigFilePath)
tokenEndpoint := GetTokenEndpointOfEnv(environment, mainConfigFilePath)
tokenEndpoint := GetInternalTokenEndpointOfEnv(environment, mainConfigFilePath)

Logln(LogPrefixInfo + "Environment: '" + environment + "'")
Logln(LogPrefixInfo+"Reg Endpoint read:", registrationEndpoint)
Expand Down

0 comments on commit eab1fb5

Please sign in to comment.