diff --git a/cli/cmd/enterprise_auth_approve.go b/cli/cmd/enterprise_auth_approve.go index dd7b6b942..15da4ad89 100644 --- a/cli/cmd/enterprise_auth_approve.go +++ b/cli/cmd/enterprise_auth_approve.go @@ -16,13 +16,13 @@ func (r *runners) InitEnterpriseAuthApprove(parent *cobra.Command) { } parent.AddCommand(cmd) - cmd.Flags().StringVar(&r.args.enterpriseAuthFingerprint, "fingerprint", "", "The fingerprint provided on auth init") + cmd.Flags().StringVar(&r.args.enterpriseAuthApproveFingerprint, "fingerprint", "", "The fingerprint provided on auth init") cmd.RunE = r.enterpriseAuthApprove } func (r *runners) enterpriseAuthApprove(cmd *cobra.Command, args []string) error { - err := r.enterpriseClient.AuthApprove(r.args.enterpriseAuthFingerprint) + err := r.enterpriseClient.AuthApprove(r.args.enterpriseAuthApproveFingerprint) if err != nil { return err } diff --git a/cli/cmd/enterprise_auth_init.go b/cli/cmd/enterprise_auth_init.go index 266ce0620..3ea31eb9a 100644 --- a/cli/cmd/enterprise_auth_init.go +++ b/cli/cmd/enterprise_auth_init.go @@ -4,24 +4,24 @@ import ( "github.com/spf13/cobra" ) -func (r *runners) InitEnterpriseAuthInit(parent *cobra.Command) *cobra.Command { +func (r *runners) InitEnterpriseAuthInit(parent *cobra.Command) { cmd := &cobra.Command{ Use: "init", Short: "initialize authentication", Long: `Create a keypair to begin authentication`, - RunE: r.enterpriseAuthInit, SilenceUsage: true, } parent.AddCommand(cmd) - return cmd + cmd.Flags().StringVar(&r.args.enterpriseAuthInitCreateOrg, "create-org", "", "If this flag is provided, a new organization will be created with the specified name. If not, the auth request will have to be approved by Replicated or your already authenticated organization") + + cmd.RunE = r.enterpriseAuthInit } func (r *runners) enterpriseAuthInit(cmd *cobra.Command, args []string) error { - err := r.enterpriseClient.AuthInit() + err := r.enterpriseClient.AuthInit(r.args.enterpriseAuthInitCreateOrg) if err != nil { return err } - return nil } diff --git a/cli/cmd/runner.go b/cli/cmd/runner.go index b9f4cf161..9d409ce93 100644 --- a/cli/cmd/runner.go +++ b/cli/cmd/runner.go @@ -35,8 +35,6 @@ type runners struct { } type runnerArgs struct { - enterpriseAuthFingerprint string - channelCreateName string channelCreateDescription string @@ -94,6 +92,10 @@ type runnerArgs struct { createInstallerPromote string createInstallerPromoteEnsureChannel bool + enterpriseAuthInitCreateOrg string + + enterpriseAuthApproveFingerprint string + enterpriseChannelCreateName string enterpriseChannelCreateDescription string diff --git a/pkg/enterpriseclient/auth.go b/pkg/enterpriseclient/auth.go index f2b7934a2..ceb4361b8 100644 --- a/pkg/enterpriseclient/auth.go +++ b/pkg/enterpriseclient/auth.go @@ -13,7 +13,7 @@ import ( "github.com/pkg/errors" ) -func (c HTTPClient) AuthInit() error { +func (c HTTPClient) AuthInit(organizationName string) error { // by default, we store the key in ~/.replicated/enterprise _, err := os.Stat(filepath.Join(homeDir(), ".replicated", "enterprise")) if err != nil && !os.IsNotExist(err) { @@ -56,24 +56,52 @@ func (c HTTPClient) AuthInit() error { return errors.Wrap(err, "failed to write public key to file") } - // send the PUBLIC key to the replicated server and return the key id - type AuthRequest struct { - PublicKeyBytes []byte `json:"publicKey"` - } - authRequest := AuthRequest{ - PublicKeyBytes: encodePublicKeyToPEM(&privateKey.PublicKey), - } + if organizationName != "" { + // --create-org flag is provided, create the organization + // send the PUBLIC key and the organization name to the replicated server and return the organization id + type CreateOrgRequest struct { + PublicKeyBytes []byte `json:"publicKey"` + OrganizationName string `json:"organizationName"` + } + createOrgRequest := CreateOrgRequest{ + PublicKeyBytes: encodePublicKeyToPEM(&privateKey.PublicKey), + OrganizationName: organizationName, + } - type AuthInitResponse struct { - Code string `json:"code"` - } - authInitResponse := AuthInitResponse{} - err = c.doJSON("POST", "/v1/auth", 201, authRequest, &authInitResponse) - if err != nil { - return errors.Wrap(err, "failed to init auth with server") + type CreateOrgResponse struct { + OrganizationID string `json:"organizationId"` + } + createOrgResponse := CreateOrgResponse{} + + err = c.doJSON("POST", "/v1/organization", 201, createOrgRequest, &createOrgResponse) + if err != nil { + return errors.Wrap(err, "failed to create organization") + } + + fmt.Printf("\nOrganization has been created successfully with the following ID: %s\n\n", createOrgResponse.OrganizationID) + } else { + // --create-org flag is NOT provided, begin authentication process + // send the PUBLIC key to the replicated server and return the key id + type AuthRequest struct { + PublicKeyBytes []byte `json:"publicKey"` + } + authRequest := AuthRequest{ + PublicKeyBytes: encodePublicKeyToPEM(&privateKey.PublicKey), + } + + type AuthInitResponse struct { + Code string `json:"code"` + } + authInitResponse := AuthInitResponse{} + + err = c.doJSON("POST", "/v1/auth", 201, authRequest, &authInitResponse) + if err != nil { + return errors.Wrap(err, "failed to init auth with server") + } + + fmt.Printf("\nYour authentication request has been submitted. Please contact Replicated at support@replicated.com to complete this request with the following code: %s\n\n", authInitResponse.Code) } - fmt.Printf("\nYour authentication request has been submitted. Please contact Replicated at support@replicated.com to complete this request with the following code: %s\n\n", authInitResponse.Code) return nil }