Skip to content

Commit

Permalink
enterprise auth init create-org flag (#126)
Browse files Browse the repository at this point in the history
* enterprise auth init create-org flag
  • Loading branch information
sgalsaleh authored Apr 22, 2020
1 parent 79e1af5 commit f12232b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cli/cmd/enterprise_auth_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions cli/cmd/enterprise_auth_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ type runners struct {
}

type runnerArgs struct {
enterpriseAuthFingerprint string

channelCreateName string
channelCreateDescription string

Expand Down Expand Up @@ -94,6 +92,10 @@ type runnerArgs struct {
createInstallerPromote string
createInstallerPromoteEnsureChannel bool

enterpriseAuthInitCreateOrg string

enterpriseAuthApproveFingerprint string

enterpriseChannelCreateName string
enterpriseChannelCreateDescription string

Expand Down
60 changes: 44 additions & 16 deletions pkg/enterpriseclient/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 [email protected] 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 [email protected] to complete this request with the following code: %s\n\n", authInitResponse.Code)
return nil
}

Expand Down

0 comments on commit f12232b

Please sign in to comment.