From 4f3de47056eca0491f03ab04dde99adfb53eac1f Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 26 Jul 2024 12:47:09 +0700 Subject: [PATCH 1/5] adding kms service --- client.go | 3 ++ go.mod | 6 +-- go.sum | 58 -------------------- kms.go | 10 ++++ kms_certificate.go | 130 +++++++++++++++++++++++++++++++++++++++++++++ kms_secret.go | 25 +++++++++ kms_test.go | 85 +++++++++++++++++++++++++++++ 7 files changed, 254 insertions(+), 63 deletions(-) create mode 100644 kms.go create mode 100644 kms_certificate.go create mode 100644 kms_secret.go create mode 100644 kms_test.go diff --git a/client.go b/client.go index 2617ced..c3eda12 100644 --- a/client.go +++ b/client.go @@ -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 @@ -80,6 +81,7 @@ type Client struct { KubernetesEngine KubernetesEngineService Service ServiceInterface Token TokenService + KMS KMSService } // Option set Client specific attributes @@ -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 } diff --git a/go.mod b/go.mod index b3716c7..cf06a6a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e4b7a13..8fdee58 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/kms.go b/kms.go new file mode 100644 index 0000000..faf3c38 --- /dev/null +++ b/kms.go @@ -0,0 +1,10 @@ +package gobizfly + +type kmsService struct { + client *Client +} + +type KMSService interface { + Certificate() *kmsCertificateService + Secret() *kmsSecretService +} diff --git a/kms_certificate.go b/kms_certificate.go new file mode 100644 index 0000000..41ecea4 --- /dev/null +++ b/kms_certificate.go @@ -0,0 +1,130 @@ +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 *KMSCertificateCreateRequest) (*KMSCertificateCreateResponse, error) + Delete(ctx context.Context, id string) error +} + +type kmsCertificateService struct { + client *Client +} + +func (k *kmsService) Certificate() *kmsCertificateService { + return &kmsCertificateService{ + client: k.client, + } +} + +type KMSCertificate struct { + ContainerId string `json:"container_id"` + Name string `json:"name"` +} + +type KMSCertificateCreateRequest struct { + CertContainer KMSCertContainer `json:"cert_container"` +} + +type KMSCertContainer struct { + Name string `json:"name"` + Certificate string `json:"certificate"` + PrivateKey string `json:"private_key"` + PrivateKeyPassphrase string `json:"private_key_passphrase"` + Intermediates string `json:"intermediates"` +} + +type KMSCertificateCreateResponse struct { + CertificateHref string `json:"certificate_href"` +} + +type KMSCertificateListResponse struct { + CertificateContrainer []*KMSCertificate `json:"certificate_container"` + Total int `json:"total"` +} + +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.CertificateContrainer, nil +} + +func (c *kmsCertificateService) Get(ctx context.Context, id string) (*KMSCertificate, 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 *KMSCertificate + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, err + } + return data, nil +} + +func (c *kmsCertificateService) Create(ctx context.Context, payload *KMSCertificateCreateRequest) (*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() +} diff --git a/kms_secret.go b/kms_secret.go new file mode 100644 index 0000000..04f4d39 --- /dev/null +++ b/kms_secret.go @@ -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) Secret() *kmsSecretService { + return &kmsSecretService{ + client: k.client, + } +} + +type KMSKey struct { + ID string `json:"id"` + Name string `json:"name"` +} diff --git a/kms_test.go b/kms_test.go new file mode 100644 index 0000000..41de9a4 --- /dev/null +++ b/kms_test.go @@ -0,0 +1,85 @@ +package gobizfly + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func ClientInit() (*Client, error) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + client, err := NewClient( + WithAPIURL("https://manage.bizflycloud.vn"), + WithProjectID("cecd854937c8421b81d1d73789acad52"), + WithRegionName("HN"), + ) + if err != nil { + fmt.Printf("Failed to create Bizfly client: %v", err) + return nil, err + } + + token, err := client.Token.Init( + ctx, + &TokenCreateRequest{ + AuthMethod: "application_credential", // application_credential + Username: "", // "" + Password: "", // "" + AppCredID: "22c2c3b3f43e4e2385dc00c575ab857d", // => from env + AppCredSecret: "WznT3PwGFdsZ-WOmDBNsr3ZXGAtJ7GuWdPfPT9evK8RcR8JCw8VmvPt7rvWQOBkzUmoCJcF5IaH2EM3j-KExDw", // => from env + }) + + if err != nil { + fmt.Printf("Failed to create token: %v", err) + return nil, err + } + client.SetKeystoneToken(token) + + return client, nil +} + +func TestListKMSCertificates1(t *testing.T) { + c, err := ClientInit() + if err != nil { + t.Fatal(err) + } + + cert, err := c.KMS.Certificate().List(ctx) + if err != nil { + t.Fatal(err) + } + + // require.NotEmpty(t, cert) + + t.Log(cert) +} + +func TestListKMSCertificates(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/ssl/certificate_container", func(writer http.ResponseWriter, request *http.Request) { + require.Equal(t, http.MethodGet, request.Method) + resp := ` +{ + "data": [ + { + "id": 1, + "name": "test", + } + ] +}` + + _, _ = fmt.Fprint(writer, resp) + }) + + certs, err := client.KMS.Certificate().List(ctx) + require.NoError(t, err) + assert.Len(t, certs, 1) + assert.LessOrEqual(t, "1", certs[0].ContainerId) +} From 747bf7850dcc90f8baebc10bf9e9b0410ae4796b Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 26 Jul 2024 13:46:27 +0700 Subject: [PATCH 2/5] fix bug create certificate --- client_test.go | 6 ++ kms_certificate.go | 46 +++++++++++---- kms_test.go | 144 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 157 insertions(+), 39 deletions(-) diff --git a/client_test.go b/client_test.go index 742afc1..119cff5 100644 --- a/client_test.go +++ b/client_test.go @@ -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 { diff --git a/kms_certificate.go b/kms_certificate.go index 41ecea4..0b4fea8 100644 --- a/kms_certificate.go +++ b/kms_certificate.go @@ -10,7 +10,7 @@ import ( type KMSCertificateService interface { List(ctx context.Context) ([]*KMSCertificate, error) Get(ctx context.Context, id string) (*KMSCertificate, error) - Create(ctx context.Context, req *KMSCertificateCreateRequest) (*KMSCertificateCreateResponse, error) + Create(ctx context.Context, req *KMSCertificateContainerCreateRequest) (*KMSCertificateCreateResponse, error) Delete(ctx context.Context, id string) error } @@ -29,16 +29,36 @@ type KMSCertificate struct { Name string `json:"name"` } -type KMSCertificateCreateRequest struct { +type KMSCertificateContainerCreateRequest struct { CertContainer KMSCertContainer `json:"cert_container"` } type KMSCertContainer struct { - Name string `json:"name"` - Certificate string `json:"certificate"` - PrivateKey string `json:"private_key"` - PrivateKeyPassphrase string `json:"private_key_passphrase"` - Intermediates string `json:"intermediates"` + 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 { @@ -50,6 +70,12 @@ type KMSCertificateListResponse struct { Total int `json:"total"` } +type KMSCertificateGetResponse struct { + ContainerId string `json:"container_id"` + Name string `json:"name"` + Certificate string `json:"certificate"` +} + const ( certificateServicePath = "/certificate_container" ) @@ -75,7 +101,7 @@ func (c *kmsCertificateService) List(ctx context.Context) ([]*KMSCertificate, er return respDecode.CertificateContrainer, nil } -func (c *kmsCertificateService) Get(ctx context.Context, id string) (*KMSCertificate, error) { +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 { @@ -87,14 +113,14 @@ func (c *kmsCertificateService) Get(ctx context.Context, id string) (*KMSCertifi } defer resp.Body.Close() - var data *KMSCertificate + 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 *KMSCertificateCreateRequest) (*KMSCertificateCreateResponse, error) { +func (c *kmsCertificateService) Create(ctx context.Context, payload *KMSCertificateContainerCreateRequest) (*KMSCertificateCreateResponse, error) { path := certificateServicePath req, err := c.client.NewRequest(ctx, http.MethodPost, kmsServiceName, path, payload) diff --git a/kms_test.go b/kms_test.go index 41de9a4..6e3c16a 100644 --- a/kms_test.go +++ b/kms_test.go @@ -3,10 +3,8 @@ package gobizfly import ( "context" "fmt" - "net/http" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -15,9 +13,9 @@ func ClientInit() (*Client, error) { defer cancel() client, err := NewClient( - WithAPIURL("https://manage.bizflycloud.vn"), - WithProjectID("cecd854937c8421b81d1d73789acad52"), - WithRegionName("HN"), + WithAPIURL(""), + WithProjectID(""), + WithRegionName(""), ) if err != nil { fmt.Printf("Failed to create Bizfly client: %v", err) @@ -27,11 +25,11 @@ func ClientInit() (*Client, error) { token, err := client.Token.Init( ctx, &TokenCreateRequest{ - AuthMethod: "application_credential", // application_credential - Username: "", // "" - Password: "", // "" - AppCredID: "22c2c3b3f43e4e2385dc00c575ab857d", // => from env - AppCredSecret: "WznT3PwGFdsZ-WOmDBNsr3ZXGAtJ7GuWdPfPT9evK8RcR8JCw8VmvPt7rvWQOBkzUmoCJcF5IaH2EM3j-KExDw", // => from env + AuthMethod: "application_credential", + Username: "", + Password: "", + AppCredID: "", + AppCredSecret: "", }) if err != nil { @@ -54,32 +52,120 @@ func TestListKMSCertificates1(t *testing.T) { t.Fatal(err) } - // require.NotEmpty(t, cert) + require.NotEmpty(t, cert) t.Log(cert) } -func TestListKMSCertificates(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/ssl/certificate_container", func(writer http.ResponseWriter, request *http.Request) { - require.Equal(t, http.MethodGet, request.Method) - resp := ` -{ - "data": [ - { - "id": 1, - "name": "test", +func TestGetKMSCertificate(t *testing.T) { + c, err := ClientInit() + if err != nil { + t.Fatal(err) } - ] -}` - _, _ = fmt.Fprint(writer, resp) + cert, err := c.KMS.Certificate().Get(ctx, "366991bc-4622-458a-bbf5-4341bef3837e") + if err != nil { + t.Fatal(err) + } + + require.NotEmpty(t, cert) + + t.Log(cert) +} + +func TestCreateKMSCertificate(t *testing.T) { + c, err := ClientInit() + if err != nil { + t.Fatal(err) + } + + cert, _ := c.KMS.Certificate().Create(ctx, &KMSCertificateContainerCreateRequest{ + CertContainer: KMSCertContainer{ + Name: "test1", + Certificate: KMSCertificateCreateReqest{ + Name: "certificate", + Payload: `-----BEGIN CERTIFICATE----- +MIID0DCCArigAwIBAgIBATANBgkqhkiG9w0BAQUFADB/MQswCQYDVQQGEwJGUjET +MBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UEBwwFUGFyaXMxDTALBgNVBAoMBERp +bWkxDTALBgNVBAsMBE5TQlUxEDAOBgNVBAMMB0RpbWkgQ0ExGzAZBgkqhkiG9w0B +CQEWDGRpbWlAZGltaS5mcjAeFw0xNDAxMjgyMDM2NTVaFw0yNDAxMjYyMDM2NTVa +MFsxCzAJBgNVBAYTAkZSMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJ +bnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxFDASBgNVBAMMC3d3dy5kaW1pLmZyMIIB +IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvpnaPKLIKdvx98KW68lz8pGa +RRcYersNGqPjpifMVjjE8LuCoXgPU0HePnNTUjpShBnynKCvrtWhN+haKbSp+QWX +SxiTrW99HBfAl1MDQyWcukoEb9Cw6INctVUN4iRvkn9T8E6q174RbcnwA/7yTc7p +1NCvw+6B/aAN9l1G2pQXgRdYC/+G6o1IZEHtWhqzE97nY5QKNuUVD0V09dc5CDYB +aKjqetwwv6DFk/GRdOSEd/6bW+20z0qSHpa3YNW6qSp+x5pyYmDrzRIR03os6Dau +ZkChSRyc/Whvurx6o85D6qpzywo8xwNaLZHxTQPgcIA5su9ZIytv9LH2E+lSwwID +AQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVy +YXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQU+tugFtyN+cXe1wxUqeA7X+yS3bgw +HwYDVR0jBBgwFoAUhMwqkbBrGp87HxfvwgPnlGgVR64wDQYJKoZIhvcNAQEFBQAD +ggEBAIEEmqqhEzeXZ4CKhE5UM9vCKzkj5Iv9TFs/a9CcQuepzplt7YVmevBFNOc0 ++1ZyR4tXgi4+5MHGzhYCIVvHo4hKqYm+J+o5mwQInf1qoAHuO7CLD3WNa1sKcVUV +vepIxc/1aHZrG+dPeEHt0MdFfOw13YdUc2FH6AqEdcEL4aV5PXq2eYR8hR4zKbc1 +fBtuqUsvA8NWSIyzQ16fyGve+ANf6vXvUizyvwDrPRv/kfvLNa3ZPnLMMxU98Mvh +PXy3PkB8++6U4Y3vdk2Ni2WYYlIls8yqbM4327IKmkDc2TimS8u60CT47mKU7aDY +cbTV5RDkrlaYwm5yqlTIglvCv7o= +-----END CERTIFICATE-----`, + }, + PrivateKey: KMSPrivateKeyCreateReqest{ + Name: "private_key", + Payload: `-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAvpnaPKLIKdvx98KW68lz8pGaRRcYersNGqPjpifMVjjE8LuC +oXgPU0HePnNTUjpShBnynKCvrtWhN+haKbSp+QWXSxiTrW99HBfAl1MDQyWcukoE +b9Cw6INctVUN4iRvkn9T8E6q174RbcnwA/7yTc7p1NCvw+6B/aAN9l1G2pQXgRdY +C/+G6o1IZEHtWhqzE97nY5QKNuUVD0V09dc5CDYBaKjqetwwv6DFk/GRdOSEd/6b +W+20z0qSHpa3YNW6qSp+x5pyYmDrzRIR03os6DauZkChSRyc/Whvurx6o85D6qpz +ywo8xwNaLZHxTQPgcIA5su9ZIytv9LH2E+lSwwIDAQABAoIBAFml8cD9a5pMqlW3 +f9btTQz1sRL4Fvp7CmHSXhvjsjeHwhHckEe0ObkWTRsgkTsm1XLu5W8IITnhn0+1 +iNr+78eB+rRGngdAXh8diOdkEy+8/Cee8tFI3jyutKdRlxMbwiKsouVviumoq3fx +OGQYwQ0Z2l/PvCwy/Y82ffq3ysC5gAJsbBYsCrg14bQo44ulrELe4SDWs5HCjKYb +EI2b8cOMucqZSOtxg9niLN/je2bo/I2HGSawibgcOdBms8k6TvsSrZMr3kJ5O6J+ +77LGwKH37brVgbVYvbq6nWPL0xLG7dUv+7LWEo5qQaPy6aXb/zbckqLqu6/EjOVe +ydG5JQECgYEA9kKfTZD/WEVAreA0dzfeJRu8vlnwoagL7cJaoDxqXos4mcr5mPDT +kbWgFkLFFH/AyUnPBlK6BcJp1XK67B13ETUa3i9Q5t1WuZEobiKKBLFm9DDQJt43 +uKZWJxBKFGSvFrYPtGZst719mZVcPct2CzPjEgN3Hlpt6fyw3eOrnoECgYEAxiOu +jwXCOmuGaB7+OW2tR0PGEzbvVlEGdkAJ6TC/HoKM1A8r2u4hLTEJJCrLLTfw++4I +ddHE2dLeR4Q7O58SfLphwgPmLDezN7WRLGr7Vyfuv7VmaHjGuC3Gv9agnhWDlA2Q +gBG9/R9oVfL0Dc7CgJgLeUtItCYC31bGT3yhV0MCgYEA4k3DG4L+RN4PXDpHvK9I +pA1jXAJHEifeHnaW1d3vWkbSkvJmgVf+9U5VeV+OwRHN1qzPZV4suRI6M/8lK8rA +Gr4UnM4aqK4K/qkY4G05LKrik9Ev2CgqSLQDRA7CJQ+Jn3Nb50qg6hFnFPafN+J7 +7juWln08wFYV4Atpdd+9XQECgYBxizkZFL+9IqkfOcONvWAzGo+Dq1N0L3J4iTIk +w56CKWXyj88d4qB4eUU3yJ4uB4S9miaW/eLEwKZIbWpUPFAn0db7i6h3ZmP5ZL8Q +qS3nQCb9DULmU2/tU641eRUKAmIoka1g9sndKAZuWo+o6fdkIb1RgObk9XNn8R4r +psv+aQKBgB+CIcExR30vycv5bnZN9EFlIXNKaeMJUrYCXcRQNvrnUIUBvAO8+jAe +CdLygS5RtgOLZib0IVErqWsP3EI1ACGuLts0vQ9GFLQGaN1SaMS40C9kvns1mlDu +LhIhYpJ8UsCVt5snWo2N+M+6ANh5tpWdQnEK6zILh4tRbuzaiHgb +-----END RSA PRIVATE KEY-----`, + }, + PrivateKeyPassphrase: KMSPrivateKeyPassphraseCreateReqest{ + Name: "passphrase", + Payload: "", + }, + // Intermediates: KMSIntermediatesCreateReqest{}, + }, }) + if err != nil { + t.Fatal(err) + } - certs, err := client.KMS.Certificate().List(ctx) require.NoError(t, err) - assert.Len(t, certs, 1) - assert.LessOrEqual(t, "1", certs[0].ContainerId) + + t.Log(cert) +} + +func TestDeleteKMSCertificate(t *testing.T) { + c, err := ClientInit() + if err != nil { + t.Fatal(err) + } + + err = c.KMS.Certificate().Delete(ctx, "35ad8118-7212-463f-8cab-c351a25ec632") + if err != nil { + t.Fatal(err) + } + + require.NoError(t, err) + + t.Log(err) } From 47885fa392f7e48ea6bc34134a3a4a512450ba40 Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 26 Jul 2024 13:54:27 +0700 Subject: [PATCH 3/5] fix typo --- kms_certificate.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kms_certificate.go b/kms_certificate.go index 0b4fea8..650ce18 100644 --- a/kms_certificate.go +++ b/kms_certificate.go @@ -25,7 +25,7 @@ func (k *kmsService) Certificate() *kmsCertificateService { } type KMSCertificate struct { - ContainerId string `json:"container_id"` + ContainerID string `json:"container_id"` Name string `json:"name"` } @@ -66,12 +66,12 @@ type KMSCertificateCreateResponse struct { } type KMSCertificateListResponse struct { - CertificateContrainer []*KMSCertificate `json:"certificate_container"` - Total int `json:"total"` + CertificateContainer []*KMSCertificate `json:"certificate_container"` + Total int `json:"total"` } type KMSCertificateGetResponse struct { - ContainerId string `json:"container_id"` + ContainerID string `json:"container_id"` Name string `json:"name"` Certificate string `json:"certificate"` } @@ -98,7 +98,7 @@ func (c *kmsCertificateService) List(ctx context.Context) ([]*KMSCertificate, er return nil, err } - return respDecode.CertificateContrainer, nil + return respDecode.CertificateContainer, nil } func (c *kmsCertificateService) Get(ctx context.Context, id string) (*KMSCertificateGetResponse, error) { From 5a082af1e00ed73490b99af653ab4f3423572598 Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 26 Jul 2024 14:49:28 +0700 Subject: [PATCH 4/5] edit interface to many object --- kms.go | 4 ++-- kms_certificate.go | 2 +- kms_secret.go | 2 +- kms_test.go | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/kms.go b/kms.go index faf3c38..154d48f 100644 --- a/kms.go +++ b/kms.go @@ -5,6 +5,6 @@ type kmsService struct { } type KMSService interface { - Certificate() *kmsCertificateService - Secret() *kmsSecretService + Certificates() *kmsCertificateService + Secrets() *kmsSecretService } diff --git a/kms_certificate.go b/kms_certificate.go index 650ce18..6c91de5 100644 --- a/kms_certificate.go +++ b/kms_certificate.go @@ -18,7 +18,7 @@ type kmsCertificateService struct { client *Client } -func (k *kmsService) Certificate() *kmsCertificateService { +func (k *kmsService) Certificates() *kmsCertificateService { return &kmsCertificateService{ client: k.client, } diff --git a/kms_secret.go b/kms_secret.go index 04f4d39..d7850ec 100644 --- a/kms_secret.go +++ b/kms_secret.go @@ -13,7 +13,7 @@ type kmsSecretService struct { client *Client } -func (k *kmsService) Secret() *kmsSecretService { +func (k *kmsService) Secrets() *kmsSecretService { return &kmsSecretService{ client: k.client, } diff --git a/kms_test.go b/kms_test.go index 6e3c16a..fabe1a4 100644 --- a/kms_test.go +++ b/kms_test.go @@ -47,7 +47,7 @@ func TestListKMSCertificates1(t *testing.T) { t.Fatal(err) } - cert, err := c.KMS.Certificate().List(ctx) + cert, err := c.KMS.Certificates().List(ctx) if err != nil { t.Fatal(err) } @@ -63,7 +63,7 @@ func TestGetKMSCertificate(t *testing.T) { t.Fatal(err) } - cert, err := c.KMS.Certificate().Get(ctx, "366991bc-4622-458a-bbf5-4341bef3837e") + cert, err := c.KMS.Certificates().Get(ctx, "366991bc-4622-458a-bbf5-4341bef3837e") if err != nil { t.Fatal(err) } @@ -79,7 +79,7 @@ func TestCreateKMSCertificate(t *testing.T) { t.Fatal(err) } - cert, _ := c.KMS.Certificate().Create(ctx, &KMSCertificateContainerCreateRequest{ + cert, _ := c.KMS.Certificates().Create(ctx, &KMSCertificateContainerCreateRequest{ CertContainer: KMSCertContainer{ Name: "test1", Certificate: KMSCertificateCreateReqest{ @@ -160,7 +160,7 @@ func TestDeleteKMSCertificate(t *testing.T) { t.Fatal(err) } - err = c.KMS.Certificate().Delete(ctx, "35ad8118-7212-463f-8cab-c351a25ec632") + err = c.KMS.Certificates().Delete(ctx, "35ad8118-7212-463f-8cab-c351a25ec632") if err != nil { t.Fatal(err) } From 3955bda2f362701a642b18bcb659c5f8b5ee2c7f Mon Sep 17 00:00:00 2001 From: virtus Date: Fri, 26 Jul 2024 15:26:35 +0700 Subject: [PATCH 5/5] update suilt test --- kms_test.go | 130 ++++++++++++++++++++++++------------------------- testlib/url.go | 4 ++ 2 files changed, 69 insertions(+), 65 deletions(-) diff --git a/kms_test.go b/kms_test.go index fabe1a4..e4e5401 100644 --- a/kms_test.go +++ b/kms_test.go @@ -1,85 +1,88 @@ package gobizfly import ( - "context" "fmt" + "net/http" "testing" + "github.com/bizflycloud/gobizfly/testlib" "github.com/stretchr/testify/require" ) -func ClientInit() (*Client, error) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - client, err := NewClient( - WithAPIURL(""), - WithProjectID(""), - WithRegionName(""), - ) - if err != nil { - fmt.Printf("Failed to create Bizfly client: %v", err) - return nil, err - } - - token, err := client.Token.Init( - ctx, - &TokenCreateRequest{ - AuthMethod: "application_credential", - Username: "", - Password: "", - AppCredID: "", - AppCredSecret: "", - }) - - if err != nil { - fmt.Printf("Failed to create token: %v", err) - return nil, err - } - client.SetKeystoneToken(token) - - return client, nil -} - func TestListKMSCertificates1(t *testing.T) { - c, err := ClientInit() - if err != nil { - t.Fatal(err) - } - - cert, err := c.KMS.Certificates().List(ctx) + setup() + defer teardown() + + mux.HandleFunc(testlib.KMSURL(certificateServicePath), func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodGet, r.Method) + resp := ` +{ + "certificate_container": [ + { + "container_id": "366991bc-4622-458a-bbf5-4341bef3837e", + "name": "test1" + } + ], + "total": 1 +} + ` + _, _ = fmt.Fprint(w, resp) + }) + cert, err := client.KMS.Certificates().List(ctx) if err != nil { t.Fatal(err) } - require.NotEmpty(t, cert) + require.NoError(t, err) + require.Equal(t, 1, len(cert)) + require.Equal(t, "test1", cert[0].Name) + require.Equal(t, "366991bc-4622-458a-bbf5-4341bef3837e", cert[0].ContainerID) t.Log(cert) } func TestGetKMSCertificate(t *testing.T) { - c, err := ClientInit() - if err != nil { - t.Fatal(err) - } - - cert, err := c.KMS.Certificates().Get(ctx, "366991bc-4622-458a-bbf5-4341bef3837e") + setup() + defer teardown() + + mux.HandleFunc(testlib.KMSURL(certificateServicePath)+"/366991bc-4622-458a-bbf5-4341bef3837e", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodGet, r.Method) + resp := ` +{ + "container_id": "366991bc-4622-458a-bbf5-4341bef3837e", + "name" : "test1", + "certificate": "certificate" +} + ` + _, _ = fmt.Fprint(w, resp) + }) + cert, err := client.KMS.Certificates().Get(ctx, "366991bc-4622-458a-bbf5-4341bef3837e") if err != nil { t.Fatal(err) } - require.NotEmpty(t, cert) + require.NoError(t, err) + require.Equal(t, "test1", cert.Name) + require.Equal(t, "366991bc-4622-458a-bbf5-4341bef3837e", cert.ContainerID) + require.Equal(t, "certificate", cert.Certificate) t.Log(cert) } func TestCreateKMSCertificate(t *testing.T) { - c, err := ClientInit() - if err != nil { - t.Fatal(err) - } - - cert, _ := c.KMS.Certificates().Create(ctx, &KMSCertificateContainerCreateRequest{ + setup() + defer teardown() + + mux.HandleFunc(testlib.KMSURL(certificateServicePath), func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodPost, r.Method) + resp := ` +{ + "certificate_href": "/kms/certificate/366991bc-4622-458a-bbf5-4341bef3837e" +} + ` + _, _ = fmt.Fprint(w, resp) + }) + cert, err := client.KMS.Certificates().Create(ctx, &KMSCertificateContainerCreateRequest{ CertContainer: KMSCertContainer{ Name: "test1", Certificate: KMSCertificateCreateReqest{ @@ -145,25 +148,22 @@ LhIhYpJ8UsCVt5snWo2N+M+6ANh5tpWdQnEK6zILh4tRbuzaiHgb // Intermediates: KMSIntermediatesCreateReqest{}, }, }) - if err != nil { - t.Fatal(err) - } require.NoError(t, err) + require.Equal(t, "/kms/certificate/366991bc-4622-458a-bbf5-4341bef3837e", cert.CertificateHref) t.Log(cert) } func TestDeleteKMSCertificate(t *testing.T) { - c, err := ClientInit() - if err != nil { - t.Fatal(err) - } + setup() + defer teardown() - err = c.KMS.Certificates().Delete(ctx, "35ad8118-7212-463f-8cab-c351a25ec632") - if err != nil { - t.Fatal(err) - } + mux.HandleFunc(testlib.KMSURL(certificateServicePath)+"/35ad8118-7212-463f-8cab-c351a25ec632", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodDelete, r.Method) + w.WriteHeader(http.StatusNoContent) + }) + err := client.KMS.Certificates().Delete(ctx, "35ad8118-7212-463f-8cab-c351a25ec632") require.NoError(t, err) diff --git a/testlib/url.go b/testlib/url.go index 9e1dea7..2221c7e 100644 --- a/testlib/url.go +++ b/testlib/url.go @@ -55,3 +55,7 @@ func VPCURL(path string) string { func DatabaseURL(path string) string { return "/api/cloud-database" + path } + +func KMSURL(path string) string { + return "/api/ssl" + path +}