Skip to content

Commit

Permalink
Merge pull request #188 from bizflycloud/kms
Browse files Browse the repository at this point in the history
Adding KMS service
  • Loading branch information
botranvan authored Jul 26, 2024
2 parents 55dd271 + 3955bda commit a496bd5
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 63 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
iamServiceName = "iam"
kubernetesServiceName = "kubernetes_engine"
loadBalancerServiceName = "load_balancer"
kmsServiceName = "key_management_service"
mediaType = "application/json; charset=utf-8"
serverServiceName = "cloud_server"
ua = "bizfly-client-go/" + version
Expand Down Expand Up @@ -80,6 +81,7 @@ type Client struct {
KubernetesEngine KubernetesEngineService
Service ServiceInterface
Token TokenService
KMS KMSService
}

// Option set Client specific attributes
Expand Down Expand Up @@ -165,6 +167,7 @@ func NewClient(options ...Option) (*Client, error) {
c.CloudLoadBalancer = &cloudLoadBalancerService{client: c}
c.Service = &service{client: c}
c.Token = &token{client: c}
c.KMS = &kmsService{client: c}
return c, nil
}

Expand Down
6 changes: 6 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func setup() {
ServiceURL: serverTest.URL + "/api/cloud-database",
Region: testRegion,
},
{
Name: "KMS",
CanonicalName: kmsServiceName,
ServiceURL: serverTest.URL + "/api/ssl",
Region: testRegion,
},
}
client.services = services
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ module github.com/bizflycloud/gobizfly

go 1.16

require (
github.com/stretchr/testify v1.4.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/tools v0.14.0 // indirect
)
require github.com/stretchr/testify v1.4.0
58 changes: 0 additions & 58 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
10 changes: 10 additions & 0 deletions kms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gobizfly

type kmsService struct {
client *Client
}

type KMSService interface {
Certificates() *kmsCertificateService
Secrets() *kmsSecretService
}
156 changes: 156 additions & 0 deletions kms_certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package gobizfly

import (
"context"
"encoding/json"
"io"
"net/http"
)

type KMSCertificateService interface {
List(ctx context.Context) ([]*KMSCertificate, error)
Get(ctx context.Context, id string) (*KMSCertificate, error)
Create(ctx context.Context, req *KMSCertificateContainerCreateRequest) (*KMSCertificateCreateResponse, error)
Delete(ctx context.Context, id string) error
}

type kmsCertificateService struct {
client *Client
}

func (k *kmsService) Certificates() *kmsCertificateService {
return &kmsCertificateService{
client: k.client,
}
}

type KMSCertificate struct {
ContainerID string `json:"container_id"`
Name string `json:"name"`
}

type KMSCertificateContainerCreateRequest struct {
CertContainer KMSCertContainer `json:"cert_container"`
}

type KMSCertContainer struct {
Name string `json:"name"`
Certificate KMSCertificateCreateReqest `json:"certificate"`
PrivateKey KMSPrivateKeyCreateReqest `json:"private_key"`
PrivateKeyPassphrase KMSPrivateKeyPassphraseCreateReqest `json:"private_key_passphrase"`
Intermediates *KMSIntermediatesCreateReqest `json:"intermediates,omitempty"`
}

type KMSCertificateCreateReqest struct {
Name string `json:"name"`
Payload string `json:"payload"`
}

type KMSPrivateKeyCreateReqest struct {
Name string `json:"name"`
Payload string `json:"payload"`
}

type KMSPrivateKeyPassphraseCreateReqest struct {
Name string `json:"name"`
Payload string `json:"payload"`
}

type KMSIntermediatesCreateReqest struct {
Name string `json:"name,omitempty"`
Payload string `json:"payload,omitempty"`
}

type KMSCertificateCreateResponse struct {
CertificateHref string `json:"certificate_href"`
}

type KMSCertificateListResponse struct {
CertificateContainer []*KMSCertificate `json:"certificate_container"`
Total int `json:"total"`
}

type KMSCertificateGetResponse struct {
ContainerID string `json:"container_id"`
Name string `json:"name"`
Certificate string `json:"certificate"`
}

const (
certificateServicePath = "/certificate_container"
)

func (c *kmsCertificateService) List(ctx context.Context) ([]*KMSCertificate, error) {
path := certificateServicePath
req, err := c.client.NewRequest(ctx, http.MethodGet, kmsServiceName, path, nil)
if err != nil {
return nil, err
}

resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var respDecode KMSCertificateListResponse
if err := json.NewDecoder(resp.Body).Decode(&respDecode); err != nil {
return nil, err
}

return respDecode.CertificateContainer, nil
}

func (c *kmsCertificateService) Get(ctx context.Context, id string) (*KMSCertificateGetResponse, error) {
path := certificateServicePath + "/" + id
req, err := c.client.NewRequest(ctx, http.MethodGet, kmsServiceName, path, nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var data *KMSCertificateGetResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}

func (c *kmsCertificateService) Create(ctx context.Context, payload *KMSCertificateContainerCreateRequest) (*KMSCertificateCreateResponse, error) {
path := certificateServicePath

req, err := c.client.NewRequest(ctx, http.MethodPost, kmsServiceName, path, payload)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var data *KMSCertificateCreateResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}

func (c *kmsCertificateService) Delete(ctx context.Context, id string) error {
path := certificateServicePath + "/" + id
req, err := c.client.NewRequest(ctx, http.MethodDelete, kmsServiceName, path, nil)
if err != nil {
return err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(io.Discard, resp.Body)

return resp.Body.Close()
}
25 changes: 25 additions & 0 deletions kms_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gobizfly

import "context"

type KMSSecretService interface {
List(ctx context.Context, page, total int) ([]*KMSKey, error)
Get(ctx context.Context, id string) (*KMSKey, error)
Create(ctx context.Context, key *KMSKey) (*KMSKey, error)
Delete(ctx context.Context, id string) error
}

type kmsSecretService struct {
client *Client
}

func (k *kmsService) Secrets() *kmsSecretService {
return &kmsSecretService{
client: k.client,
}
}

type KMSKey struct {
ID string `json:"id"`
Name string `json:"name"`
}
Loading

0 comments on commit a496bd5

Please sign in to comment.