diff --git a/docs/commands/rhoas_generate-config.md b/docs/commands/rhoas_generate-config.md index a40d22be8..3c8d6a702 100644 --- a/docs/commands/rhoas_generate-config.md +++ b/docs/commands/rhoas_generate-config.md @@ -11,7 +11,7 @@ You must specify an output format into which the credentials will be stored: - env (default): Store configurations in an env file as environment variables - json: Store configurations in a JSON file - properties: Store configurations in a properties file, which is typically used in Java-related technologies -- configmap: Store configurations in a Kubernetes ConfigMap file +- secret: Store configurations in a Kubernetes secret file ``` @@ -27,8 +27,8 @@ $ rhoas generate-config --type json ## Generate configurations for the current service context in env format and save it in specified path $ rhoas generate-config --type env --output-file ./configs/.env -## Generate configurations for a specified context as Kubernetes ConfigMap -$ rhoas generate-config --name qaprod --type configmap +## Generate configurations for a specified context as Kubernetes secret +$ rhoas generate-config --name qaprod --type secret ``` diff --git a/pkg/cmd/generate/build-configs.go b/pkg/cmd/generate/build-configs.go index 0a57a519d..2dfb85150 100644 --- a/pkg/cmd/generate/build-configs.go +++ b/pkg/cmd/generate/build-configs.go @@ -4,21 +4,51 @@ import ( "fmt" "time" + "github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + + kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" ) type configValues struct { - KafkaHost string - RegistryURL string + KafkaHost string + RegistryURL string + ClientID string + ClientSecret string + TokenURL string // Optional Name string } +func createServiceAccount(opts *options, shortDescription string) (*kafkamgmtclient.ServiceAccount, error) { + conn, err := opts.Connection() + if err != nil { + return nil, err + } + serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest{Name: shortDescription} + + serviceacct, httpRes, err := conn.API(). + ServiceAccountMgmt(). + CreateServiceAccount(opts.Context). + ServiceAccountRequest(serviceAccountPayload). + Execute() + + if httpRes != nil { + defer httpRes.Body.Close() + } + + if err != nil { + return nil, err + } + + return &serviceacct, nil +} + // BuildConfiguration builds the configs for the service context func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) error { @@ -34,7 +64,6 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) configurations := &configValues{} var serviceAvailable bool - var err error if svcConfig.KafkaID != "" { kafkaInstance, newErr := contextutil.GetCurrentKafkaInstance(factory) @@ -60,7 +89,29 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) return opts.localizer.MustLocalizeError("generate.log.info.noServices") } configInstanceName := fmt.Sprintf("%s-%v", opts.name, time.Now().Unix()) + serviceAccount, err := createServiceAccount(opts, configInstanceName) + if err != nil { + return err + } + + opts.Logger.Info( + icon.SuccessPrefix(), + opts.localizer.MustLocalize("serviceAccount.create.log.info.createdSuccessfully", localize.NewEntry("ID", serviceAccount.GetId())), + ) + + conn, err := opts.Connection() + if err != nil { + return err + } + + providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context) + if err != nil { + return err + } + configurations.ClientID = serviceAccount.GetClientId() + configurations.ClientSecret = serviceAccount.GetClientSecret() + configurations.TokenURL = providerUrls.GetTokenUrl() configurations.Name = configInstanceName var fileName string diff --git a/pkg/cmd/generate/configurations.go b/pkg/cmd/generate/configurations.go index c17fcd9c3..c68a9ac5e 100644 --- a/pkg/cmd/generate/configurations.go +++ b/pkg/cmd/generate/configurations.go @@ -15,16 +15,16 @@ const ( envFormat = "env" jsonFormat = "json" propertiesFormat = "properties" - configmapFormat = "configmap" + secretFormat = "secret" ) -var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, configmapFormat} +var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, secretFormat} var ( - envConfig = template.Must(template.New(envFormat).Parse(templateEnv)) - jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON)) - propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties)) - configMapTemplateConfig = template.Must(template.New(configmapFormat).Parse(templateConfigMap)) + envConfig = template.Must(template.New(envFormat).Parse(templateEnv)) + jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON)) + propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties)) + secretTemplateConfig = template.Must(template.New(secretFormat).Parse(templateSecret)) ) // WriteConfig saves the configurations to a file @@ -62,8 +62,8 @@ func getDefaultPath(configType string) (filePath string) { filePath = "rhoas.properties" case jsonFormat: filePath = "rhoas.json" - case configmapFormat: - filePath = "rhoas-services.yaml" + case secretFormat: + filePath = "rhoas-services-secret.yaml" } pwd, err := os.Getwd() @@ -84,8 +84,8 @@ func getFileFormat(configType string) (template *template.Template) { template = propertiesConfig case jsonFormat: template = jsonConfig - case configmapFormat: - template = configMapTemplateConfig + case secretFormat: + template = secretTemplateConfig } return template diff --git a/pkg/cmd/generate/generate-config.go b/pkg/cmd/generate/generate-config.go index febf61070..c489defb4 100644 --- a/pkg/cmd/generate/generate-config.go +++ b/pkg/cmd/generate/generate-config.go @@ -63,8 +63,7 @@ func NewGenerateCommand(f *factory.Factory) *cobra.Command { flags.AddContextName(&opts.name) flags.StringVar(&opts.configType, "type", "", opts.localizer.MustLocalize("generate.flag.type")) cmd.Flags().BoolVar(&opts.overwrite, "overwrite", false, opts.localizer.MustLocalize("generate.flag.overwrite.description")) - flags.StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description")) - + cmd.Flags().StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description")) _ = cmd.MarkFlagRequired("type") flagutil.EnableStaticFlagCompletion(cmd, "type", configurationTypes) diff --git a/pkg/cmd/generate/templates.go b/pkg/cmd/generate/templates.go index 588f9485a..6f796f3f9 100644 --- a/pkg/cmd/generate/templates.go +++ b/pkg/cmd/generate/templates.go @@ -15,6 +15,10 @@ var ( SERVICE_REGISTRY_CORE_PATH=` + registrycmdutil.REGISTRY_CORE_PATH + ` SERVICE_REGISTRY_COMPAT_PATH=` + registrycmdutil.REGISTRY_COMPAT_PATH + ` {{end}} + ## Authentication Configuration + RHOAS_CLIENT_ID={{.ClientID}} + RHOAS_CLIENT_SECRET={{.ClientSecret}} + RHOAS_OAUTH_TOKEN_URL={{.TokenURL}} `) templateJSON = heredoc.Doc(` @@ -22,7 +26,10 @@ var ( {{if .KafkaHost}}"kafkaHost":"{{.KafkaHost}}", {{end}}{{if .RegistryURL}}"serviceRegistryUrl":"{{.RegistryURL}}", "serviceRegistryCorePath":"` + registrycmdutil.REGISTRY_CORE_PATH + `", - "serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `"{{end}} + "serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `", + {{end}}"rhoasClientID":"{{.ClientID}}", + "rhoasClientSecret":"{{.ClientSecret}}", + "rhoasOauthTokenUrl":"{{.TokenURL}}" } `) @@ -30,24 +37,35 @@ var ( ## Generated by rhoas cli {{if .KafkaHost}}## Kafka Configuration kafkaHost={{.KafkaHost}} - {{end}}{{if .RegistryURL}}## Service Registry Configuration + {{end}}{{if .RegistryURL}} ## Service Registry Configuration serviceRegistryUrl={{.RegistryURL}} serviceRegistryCorePath=` + registrycmdutil.REGISTRY_CORE_PATH + ` - serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + `{{end}} + serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + ` + {{end}} + ## Authentication Configuration + rhoasClientID={{.ClientID}} + rhoasClientSecret={{.ClientSecret}} + rhoasOauthTokenUrl={{.TokenURL}} `) - templateConfigMap = heredoc.Doc(` + templateSecret = heredoc.Doc(` apiVersion: v1 - kind: ConfigMap + kind: Secret metadata: name: {{.Name}} - data: + type: Opaque + stringData: {{if .KafkaHost}}## Kafka Configuration KAFKA_HOST: {{.KafkaHost}} {{end}} {{if .RegistryURL}}## Service Registry Configuration SERVICE_REGISTRY_URL: {{.RegistryURL}} SERVICE_REGISTRY_COMPAT_PATH: ` + registrycmdutil.REGISTRY_COMPAT_PATH + ` - SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + `{{end}} + SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + ` + {{end}} + ## Authentication Configuration + RHOAS_CLIENT_ID: {{.ClientID}} + RHOAS_CLIENT_SECRET: {{.ClientSecret}} + RHOAS_OAUTH_TOKEN_URL: {{.TokenURL}} `) ) diff --git a/pkg/core/localize/locales/en/cmd/generate_config.en.toml b/pkg/core/localize/locales/en/cmd/generate_config.en.toml index 2c64a3fe6..089b65cce 100644 --- a/pkg/core/localize/locales/en/cmd/generate_config.en.toml +++ b/pkg/core/localize/locales/en/cmd/generate_config.en.toml @@ -10,7 +10,7 @@ You must specify an output format into which the credentials will be stored: - env (default): Store configurations in an env file as environment variables - json: Store configurations in a JSON file - properties: Store configurations in a properties file, which is typically used in Java-related technologies -- configmap: Store configurations in a Kubernetes ConfigMap file +- secret: Store configurations in a Kubernetes secret file ''' [generate.cmd.example] @@ -21,8 +21,8 @@ $ rhoas generate-config --type json ## Generate configurations for the current service context in env format and save it in specified path $ rhoas generate-config --type env --output-file ./configs/.env -## Generate configurations for a specified context as Kubernetes ConfigMap -$ rhoas generate-config --name qaprod --type configmap +## Generate configurations for a specified context as Kubernetes secret +$ rhoas generate-config --name qaprod --type secret ''' [generate.flag.type] @@ -43,8 +43,4 @@ one = 'file {{.FilePath}} already exists. Use --overwrite to overwrite the file, one='No services available to generate configurations' [generate.log.info.credentialsSaved] -one=''' -Configurations successfully saved to "{{.FilePath}}" - -You can now create new service accounts or use existing ones to connect to the service(s) -''' \ No newline at end of file +one='Configurations successfully saved to "{{.FilePath}}"' \ No newline at end of file