Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMS: keys refactoring #749

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions acceptance/openstack/kms/v1/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,37 @@ func TestKmsKeysLifecycle(t *testing.T) {
KeyAlias: kmsID,
KeyDescription: "some description",
}
createKey, err := keys.Create(client, createOpts).ExtractKeyInfo()
createKey, err := keys.Create(client, createOpts)
th.AssertNoErr(t, err)

defer func() {
deleteOpts := keys.DeleteOpts{
KeyID: createKey.KeyID,
PendingDays: "7",
}
_, err := keys.Delete(client, deleteOpts).Extract()
_, err := keys.Delete(client, deleteOpts)
th.AssertNoErr(t, err)
}()

keyGet, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
keyGet, err := keys.Get(client, createKey.KeyID)
th.AssertNoErr(t, err)
th.AssertEquals(t, createOpts.KeyAlias, keyGet.KeyAlias)
th.AssertEquals(t, keyGet.KeyState, "2")
th.AssertEquals(t, keyGet.KeyState, "4")

deleteOpts := keys.DeleteOpts{
KeyID: createKey.KeyID,
PendingDays: "7",
}
_, err = keys.Delete(client, deleteOpts).Extract()
th.AssertNoErr(t, err)

keyGetDeleted, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
th.AssertNoErr(t, err)
th.AssertEquals(t, keyGetDeleted.KeyState, "4")

cancelDeleteOpts := keys.CancelDeleteOpts{
KeyID: createKey.KeyID,
}
_, err = keys.CancelDelete(client, cancelDeleteOpts).Extract()
_, err = keys.CancelDelete(client, createKey.KeyID)
th.AssertNoErr(t, err)

_, err = keys.EnableKey(client, createKey.KeyID).Extract()
_, err = keys.EnableKey(client, createKey.KeyID)
th.AssertNoErr(t, err)

keyGetEnabled, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
keyGetEnabled, err := keys.Get(client, createKey.KeyID)
th.AssertNoErr(t, err)
th.AssertEquals(t, keyGetEnabled.KeyState, "2")

_, err = keys.DisableKey(client, createKey.KeyID).Extract()
_, err = keys.DisableKey(client, createKey.KeyID)
th.AssertNoErr(t, err)

keyGetDisabled, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
keyGetDisabled, err := keys.Get(client, createKey.KeyID)
th.AssertNoErr(t, err)
th.AssertEquals(t, keyGetDisabled.KeyState, "3")
}
Expand Down
32 changes: 32 additions & 0 deletions openstack/kms/v1/keys/CancelDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func CancelDelete(client *golangsdk.ServiceClient, keyID string) (*Key, error) {
opts := struct {
KeyID string `json:"key_id"`
}{
KeyID: keyID,
}

b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "cancel-key-deletion"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res Key

err = extract.Into(raw.Body, &res)
return &res, err
}
24 changes: 24 additions & 0 deletions openstack/kms/v1/keys/CancelKeyDeletion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
)

func CancelDeletion(client *golangsdk.ServiceClient, keyID string) error {
opts := struct {
KeyID string `json:"key_id"`
}{
KeyID: keyID,
}

b, err := build.RequestBody(opts, "")
if err != nil {
return err
}

_, err = client.Post(client.ServiceURL("kms", "cancel-key-deletion"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return err
}
38 changes: 38 additions & 0 deletions openstack/kms/v1/keys/Create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type CreateOpts struct {
// Alias of a CMK
KeyAlias string `json:"key_alias" required:"true"`
// CMK description
KeyDescription string `json:"key_description,omitempty"`
// Region where a CMK resides
Realm string `json:"realm,omitempty"`
// Purpose of a CMK (The default value is Encrypt_Decrypt)
KeyUsage string `json:"key_usage,omitempty"`
}

func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Key, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "create-key"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})

if err != nil {
return nil, err
}

var res Key

err = extract.IntoStructPtr(raw.Body, &res, "key_info")
return &res, err
}
41 changes: 41 additions & 0 deletions openstack/kms/v1/keys/DataEncrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type DataEncryptOpts struct {

Check failure on line 9 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / unit tests

other declaration of DataEncryptOpts

Check failure on line 9 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / golangci-lint

other declaration of DataEncryptOpts

Check failure on line 9 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / acceptance tests

other declaration of DataEncryptOpts

Check failure on line 9 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / vet

other declaration of DataEncryptOpts
// ID of a CMK
KeyID string `json:"key_id" required:"true"`
// Encryption context
EncryptionContext string `json:"encryption_context,omitempty"`
// Data key length
DataKeyLength string `json:"datakey_length,omitempty"`
}

type DataKey struct {

Check failure on line 18 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / unit tests

other declaration of DataKey

Check failure on line 18 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / golangci-lint

other declaration of DataKey

Check failure on line 18 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / acceptance tests

other declaration of DataKey

Check failure on line 18 in openstack/kms/v1/keys/DataEncrypt.go

View workflow job for this annotation

GitHub Actions / vet

other declaration of DataKey
// Current ID of a CMK
KeyID string `json:"key_id"`
PlainText string `json:"plain_text"`
CipherText string `json:"cipher_text"`
}

func DataEncrypt(client *golangsdk.ServiceClient, opts DataEncryptOpts) (*DataKey, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "create-datakey"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res DataKey
err = extract.Into(raw.Body, &res)
return &res, err
}
43 changes: 43 additions & 0 deletions openstack/kms/v1/keys/DataEncryptGet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type DataEncryptOpts struct {

Check failure on line 9 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / unit tests

DataEncryptOpts redeclared in this block

Check failure on line 9 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / golangci-lint

DataEncryptOpts redeclared in this block

Check failure on line 9 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / acceptance tests

DataEncryptOpts redeclared in this block

Check failure on line 9 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / vet

DataEncryptOpts redeclared in this block
// ID of a CMK
KeyID string `json:"key_id" required:"true"`
// CMK description
EncryptionContext string `json:"encryption_context,omitempty"`
// 36-byte serial number of a request message
DatakeyLength string `json:"datakey_length,omitempty"`
}

func DataEncryptGet(client *golangsdk.ServiceClient, opts DataEncryptOpts) (*DataKey, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "create-datakey"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})

if err != nil {
return nil, err
}

var res DataKey

err = extract.Into(raw.Body, &res)
return &res, err
}

type DataKey struct {

Check failure on line 38 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / unit tests

DataKey redeclared in this block

Check failure on line 38 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / golangci-lint

DataKey redeclared in this block

Check failure on line 38 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / acceptance tests

DataKey redeclared in this block

Check failure on line 38 in openstack/kms/v1/keys/DataEncryptGet.go

View workflow job for this annotation

GitHub Actions / vet

DataKey redeclared in this block
// Current ID of a CMK
KeyID string `json:"key_id"`
PlainText string `json:"plain_text"`
CipherText string `json:"cipher_text"`
}
40 changes: 40 additions & 0 deletions openstack/kms/v1/keys/Delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type DeleteOpts struct {

Check failure on line 9 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / unit tests

other declaration of DeleteOpts

Check failure on line 9 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / golangci-lint

other declaration of DeleteOpts

Check failure on line 9 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / acceptance tests

other declaration of DeleteOpts

Check failure on line 9 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / vet

other declaration of DeleteOpts
// ID of a CMK
KeyID string `json:"key_id" required:"true"`
// Number of days after which a CMK is scheduled to be deleted
// (The value ranges from 7 to 1096.)
PendingDays string `json:"pending_days" required:"true"`
}

func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) (*UpdateKeyState, error) {

Check failure on line 17 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / unit tests

other declaration of Delete

Check failure on line 17 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / golangci-lint

other declaration of Delete

Check failure on line 17 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / acceptance tests

other declaration of Delete

Check failure on line 17 in openstack/kms/v1/keys/Delete.go

View workflow job for this annotation

GitHub Actions / vet

other declaration of Delete
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "schedule-key-deletion"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})

if err != nil {
return nil, err
}

var res UpdateKeyState

err = extract.Into(raw.Body, &res)
return &res, err
}

type UpdateKeyState struct {
KeyID string `json:"key_id"`
KeyState string `json:"key_state"`
}
26 changes: 26 additions & 0 deletions openstack/kms/v1/keys/DeleteKey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
)

type DeleteOpts struct {

Check failure on line 8 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / unit tests

DeleteOpts redeclared in this block

Check failure on line 8 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

DeleteOpts redeclared in this block

Check failure on line 8 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / acceptance tests

DeleteOpts redeclared in this block

Check failure on line 8 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / vet

DeleteOpts redeclared in this block
// ID of a CMK
KeyID string `json:"key_id" required:"true"`
// Number of days after which a CMK is scheduled to be deleted
// (The value ranges from 7 to 1096.)
PendingDays string `json:"pending_days" required:"true"`
}

func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) error {

Check failure on line 16 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / unit tests

Delete redeclared in this block

Check failure on line 16 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Delete redeclared in this block

Check failure on line 16 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / acceptance tests

Delete redeclared in this block

Check failure on line 16 in openstack/kms/v1/keys/DeleteKey.go

View workflow job for this annotation

GitHub Actions / vet

Delete redeclared in this block
b, err := build.RequestBody(opts, "")
if err != nil {
return err
}

_, err = client.Post(client.ServiceURL("kms", "schedule-key-deletion"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return err
}
32 changes: 32 additions & 0 deletions openstack/kms/v1/keys/DisableKey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

func DisableKey(client *golangsdk.ServiceClient, keyID string) (*UpdateKeyState, error) {
opts := struct {
KeyID string `json:"key_id"`
}{
KeyID: keyID,
}

b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

raw, err := client.Post(client.ServiceURL("kms", "disable-key"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res UpdateKeyState

err = extract.IntoStructPtr(raw.Body, &res, "key_info")
return &res, err
}
24 changes: 24 additions & 0 deletions openstack/kms/v1/keys/DisableKeyRotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keys

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
)

func DisableKeyRotation(client *golangsdk.ServiceClient, keyID string) error {
opts := struct {
KeyID string `json:"key_id"`
}{
KeyID: keyID,
}

b, err := build.RequestBody(opts, "")
if err != nil {
return err
}

_, err = client.Post(client.ServiceURL("kms", "disable-key-rotation"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return err
}
Loading
Loading