Skip to content

Commit

Permalink
#6: Added support for fetching trusted CAs from clusters.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett19 committed Nov 2, 2023
1 parent 2a61628 commit a1c34cd
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 1 deletion.
32 changes: 32 additions & 0 deletions cmd/certificates-getca.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"go.uber.org/zap"
)

var certificatesGetCaCmd = &cobra.Command{
Use: "get-ca",
Short: "Fetches the CA certificate",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
helper := CmdHelper{}
logger := helper.GetLogger()
ctx := helper.GetContext()

_, deployer, cluster := helper.IdentifyCluster(ctx, args[0])

cert, err := deployer.GetCertificate(ctx, cluster.GetID())
if err != nil {
logger.Fatal("failed to get certificate", zap.Error(err))
}

fmt.Printf("%s\n", cert)
},
}

func init() {
certificatesCmd.AddCommand(certificatesGetCaCmd)
}
15 changes: 15 additions & 0 deletions cmd/certificates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/cobra"
)

var certificatesCmd = &cobra.Command{
Use: "certificates",
Short: "Provides access to tools related to Couchbase Cloud certificates",
Run: nil,
}

func init() {
rootCmd.AddCommand(certificatesCmd)
}
18 changes: 17 additions & 1 deletion deployment/clouddeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ func (p *Deployer) ListUsers(ctx context.Context, clusterID string) ([]deploymen
for _, user := range resp.Data {
canRead := false
canWrite := false
for permName, _ := range user.Data.Permissions {
for permName := range user.Data.Permissions {
if permName == "data_writer" {
canWrite = true
} else if permName == "data_reader" {
Expand Down Expand Up @@ -1071,3 +1071,19 @@ func (p *Deployer) DeleteBucket(ctx context.Context, clusterID string, bucketNam

return nil
}

func (p *Deployer) GetCertificate(ctx context.Context, clusterID string) (string, error) {
clusterInfo, err := p.getCluster(ctx, clusterID)
if err != nil {
return "", err
}

resp, err := p.mgr.Client.GetTrustedCAs(ctx, clusterInfo.Cluster.Id)
if err != nil {
return "", errors.Wrap(err, "failed to get trusted CAs")
}

lastCert := (*resp)[len(*resp)-1]

return strings.TrimSpace(lastCert.Pem), nil
}
1 change: 1 addition & 0 deletions deployment/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ type Deployer interface {
ListBuckets(ctx context.Context, clusterID string) ([]BucketInfo, error)
CreateBucket(ctx context.Context, clusterID string, opts *CreateBucketOptions) error
DeleteBucket(ctx context.Context, clusterID string, bucketName string) error
GetCertificate(ctx context.Context, clusterID string) (string, error)
}
16 changes: 16 additions & 0 deletions deployment/dockerdeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,19 @@ func (d *Deployer) DeleteBucket(ctx context.Context, clusterID string, bucketNam

return nil
}

func (d *Deployer) GetCertificate(ctx context.Context, clusterID string) (string, error) {
controller, err := d.getController(ctx, clusterID)
if err != nil {
return "", errors.Wrap(err, "failed to get cluster controller")
}

resp, err := controller.Controller().GetTrustedCAs(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to get trusted CAs")
}

lastCert := (*resp)[len(*resp)-1]

return strings.TrimSpace(lastCert.Pem), nil
}
4 changes: 4 additions & 0 deletions deployment/localdeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ func (d *Deployer) CreateBucket(ctx context.Context, clusterID string, opts *dep
func (d *Deployer) DeleteBucket(ctx context.Context, clusterID string, bucketName string) error {
return errors.New("localdeploy does not support user management")
}

func (p *Deployer) GetCertificate(ctx context.Context, clusterID string) (string, error) {
return "", errors.New("localdeploy does not support getting the CA certificate")
}
25 changes: 25 additions & 0 deletions utils/capellacontrol/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,28 @@ func (c *Controller) DeleteBucket(

return nil
}

type GetTrustedCAsResponse []GetTrustedCAsResponse_Certificate

type GetTrustedCAsResponse_Certificate struct {
ID int `json:"id"`
Subject string `json:"subject"`
NotBefore string `json:"notBefore"`
NotAfter string `json:"notAfter"`
Pem string `json:"pem"`
}

func (c *Controller) GetTrustedCAs(
ctx context.Context,
clusterID string,
) (*GetTrustedCAsResponse, error) {
resp := &GetTrustedCAsResponse{}

path := fmt.Sprintf("/v2/databases/%s/proxy/pools/default/trustedCAs", clusterID)
err := c.doBasicReq(ctx, false, "GET", path, nil, &resp)
if err != nil {
return nil, err
}

return resp, err
}
22 changes: 22 additions & 0 deletions utils/clustercontrol/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,25 @@ func (c *Controller) DeleteBucket(ctx context.Context, bucketName string) error

return nil
}

type GetTrustedCAsResponse []GetTrustedCAsResponse_Certificate

type GetTrustedCAsResponse_Certificate struct {
ID int `json:"id"`
Subject string `json:"subject"`
NotBefore string `json:"notBefore"`
NotAfter string `json:"notAfter"`
Pem string `json:"pem"`
}

func (c *Controller) GetTrustedCAs(ctx context.Context) (*GetTrustedCAsResponse, error) {
resp := &GetTrustedCAsResponse{}

path := "/pools/default/trustedCAs"
err := c.doGet(ctx, path, &resp)
if err != nil {
return nil, err
}

return resp, nil
}

0 comments on commit a1c34cd

Please sign in to comment.