Skip to content

Commit

Permalink
cocli: allow specifying api-server via config file
Browse files Browse the repository at this point in the history
Make api-server configurable via the config file, as well as the command
line flag. This removes the need to specify --api-server flag on each
corim submit sub-command invocation (given that this setting is going to
be fairly static for a particular setup).

Signed-off-by: Sergei Trofimov <[email protected]>
  • Loading branch information
setrofim committed Aug 29, 2023
1 parent 613c789 commit 2e8a993
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cocli/cmd/corimSubmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

var (
corimFile *string
apiServer *string
mediaType *string
apiServer string
)

var (
Expand Down Expand Up @@ -54,17 +54,17 @@ func NewCorimSubmitCmd(submitter ISubmitter) *cobra.Command {
return fmt.Errorf("read CoRIM payload failed: %w", err)
}

if err = provisionData(data, submitter, *apiServer, *mediaType); err != nil {
if err = provisionData(data, submitter, apiServer, *mediaType); err != nil {
return fmt.Errorf("submit CoRIM payload failed reason: %w", err)
}
return nil
},
}

corimFile = cmd.Flags().StringP("corim-file", "f", "", "name of the CoRIM file in CBOR format")
apiServer = cmd.Flags().StringP("api-server", "s", "", "API server where to submit the corim file")
mediaType = cmd.Flags().StringP("media-type", "m", "", "media type of the CoRIM file")

cmd.Flags().StringP("api-server", "s", "", "API server where to submit the corim file")
cmd.Flags().VarP(&authMethod, "auth", "a",
`authentication method, must be one of "none"/"passthrough", "basic", "oauth2"`)
cmd.Flags().StringP("client-id", "C", "", "OAuth2 client ID")
Expand All @@ -73,7 +73,9 @@ func NewCorimSubmitCmd(submitter ISubmitter) *cobra.Command {
cmd.Flags().StringP("username", "U", "", "service username")
cmd.Flags().StringP("password", "P", "", "service password")

err := viper.BindPFlag("auth", cmd.Flags().Lookup("auth"))
err := viper.BindPFlag("api_server", cmd.Flags().Lookup("api-server"))
cobra.CheckErr(err)
err = viper.BindPFlag("auth", cmd.Flags().Lookup("auth"))
cobra.CheckErr(err)
err = viper.BindPFlag("client_id", cmd.Flags().Lookup("client-id"))
cobra.CheckErr(err)
Expand All @@ -93,10 +95,12 @@ func checkSubmitArgs() error {
if corimFile == nil || *corimFile == "" {
return errors.New("no CoRIM input file supplied")
}
if apiServer == nil || *apiServer == "" {

apiServer = viper.GetString("api_server")
if apiServer == "" {
return errors.New("no API server supplied")
}
u, err := url.Parse(*apiServer)
u, err := url.Parse(apiServer)
if err != nil || !u.IsAbs() {
return fmt.Errorf("malformed API server URL")
}
Expand Down
3 changes: 3 additions & 0 deletions cocli/data/config/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# do not need this configuration for creating or monipulating corims and
# related objects locally.

# API Server submit endpoint URL.
api_server: https://veraison.example/endorsement-provisioning/v1/submit

# Authentication method used by the remote service.
auth: none # may also be "basic" or "oauth2"

Expand Down

0 comments on commit 2e8a993

Please sign in to comment.