Skip to content

Commit

Permalink
feat(generate-config): replace secret with configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 committed Jul 21, 2022
1 parent 200f22f commit c7ea1ac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 122 deletions.
12 changes: 4 additions & 8 deletions docs/commands/rhoas_generate-config.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 2 additions & 67 deletions pkg/cmd/generate/build-configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,21 @@ 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
ClientID string
ClientSecret string
TokenURL string
KafkaHost string
RegistryURL 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 {

Expand All @@ -64,7 +34,6 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options)
configurations := &configValues{}

var serviceAvailable bool
var serviceAccount *kafkamgmtclient.ServiceAccount
var err error

if svcConfig.KafkaID != "" {
Expand Down Expand Up @@ -92,40 +61,6 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options)
}
configInstanceName := fmt.Sprintf("%s-%v", opts.name, time.Now().Unix())

if opts.generateAuth {
serviceAccount, err = createServiceAccount(opts, configInstanceName)
if err != nil {
return err
}

configurations.ClientID = serviceAccount.GetClientId()
configurations.ClientSecret = serviceAccount.GetClientSecret()

opts.Logger.Info(
icon.SuccessPrefix(),
opts.localizer.MustLocalize("serviceAccount.create.log.info.createdSuccessfully", localize.NewEntry("ID", serviceAccount.GetId())),
)
} else {
configurations.ClientID = opts.clientID

if opts.clientSecretStdin {
fmt.Scanln(&opts.clientSecret)
}

configurations.ClientSecret = opts.clientSecret
}

conn, err := opts.Connection()
if err != nil {
return err
}

providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context)
if err != nil {
return err
}

configurations.TokenURL = providerUrls.GetTokenUrl()
configurations.Name = configInstanceName

var fileName string
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/generate/configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const (
envFormat = "env"
jsonFormat = "json"
propertiesFormat = "properties"
secretFormat = "secret"
configmapFormat = "configmap"
)

var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, secretFormat}
var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, configmapFormat}

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))
secretTemplateConfig = template.Must(template.New(secretFormat).Parse(templateSecret))
envConfig = template.Must(template.New(envFormat).Parse(templateEnv))
jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON))
propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties))
congMapTemplateConfig = template.Must(template.New(configmapFormat).Parse(templateConfigMap))
)

// WriteConfig saves the configurations to a file
Expand Down Expand Up @@ -62,8 +62,8 @@ func getDefaultPath(configType string) (filePath string) {
filePath = "rhoas.properties"
case jsonFormat:
filePath = "rhoas.json"
case secretFormat:
filePath = "rhoas-services-secret.yaml"
case configmapFormat:
filePath = "rhoas-services.yaml"
}

pwd, err := os.Getwd()
Expand All @@ -84,8 +84,8 @@ func getFileFormat(configType string) (template *template.Template) {
template = propertiesConfig
case jsonFormat:
template = jsonConfig
case secretFormat:
template = secretTemplateConfig
case configmapFormat:
template = congMapTemplateConfig
}

return template
Expand Down
16 changes: 4 additions & 12 deletions pkg/cmd/generate/generate-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ type options struct {
Config config.IConfig
ServiceContext servicecontext.IContext

name string
fileName string
configType string
overwrite bool
generateAuth bool
clientID string
clientSecret string
clientSecretStdin bool
name string
fileName string
configType string
overwrite bool
}

// NewGenerateCommand creates configuration files for service context
Expand Down Expand Up @@ -68,10 +64,6 @@ func NewGenerateCommand(f *factory.Factory) *cobra.Command {
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"))
flags.BoolVar(&opts.generateAuth, "generate-auth", false, "Create service account")
flags.StringVar(&opts.clientID, "client-id", "", "Client ID of the service account")
flags.StringVar(&opts.clientSecret, "client-secret", "", "Client secret of the service account")
flags.BoolVar(&opts.clientSecretStdin, "client-secret-stdin", false, "Take the client secret from stdin")

_ = cmd.MarkFlagRequired("type")

Expand Down
32 changes: 7 additions & 25 deletions pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,39 @@ 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(`
{
{{if .KafkaHost}}"kafkaHost":"{{.KafkaHost}}",
{{end}}{{if .RegistryURL}}"serviceRegistryUrl":"{{.RegistryURL}}",
"serviceRegistryCorePath":"` + registrycmdutil.REGISTRY_CORE_PATH + `",
"serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `",
{{end}}"rhoasClientID":"{{.ClientID}}",
"rhoasClientSecret":"{{.ClientSecret}}",
"rhoasOauthTokenUrl":"{{.TokenURL}}"
"serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `"{{end}}
}
`)

templateProperties = heredoc.Doc(`
## 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}}
## Authentication Configuration
rhoasClientID={{.ClientID}}
rhoasClientSecret={{.ClientSecret}}
rhoasOauthTokenUrl={{.TokenURL}}
serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + `{{end}}
`)

templateSecret = heredoc.Doc(`
templateConfigMap = heredoc.Doc(`
apiVersion: v1
kind: Secret
kind: ConfigMap
metadata:
name: {{.Name}}
type: Opaque
stringData:
data:
{{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}}
## Authentication Configuration
RHOAS_CLIENT_ID: {{.ClientID}}
RHOAS_CLIENT_SECRET: {{.ClientSecret}}
RHOAS_OAUTH_TOKEN_URL: {{.TokenURL}}
SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + `{{end}}
`)
)

0 comments on commit c7ea1ac

Please sign in to comment.