1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
|
// This file was automatically generated. DO NOT EDIT.
// If you have any remark or suggestion do not hesitate to open an issue.
// Package key_manager provides methods and message types of the key_manager v1alpha1 API.
package key_manager
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/scaleway/scaleway-sdk-go/errors"
"github.com/scaleway/scaleway-sdk-go/marshaler"
"github.com/scaleway/scaleway-sdk-go/namegenerator"
"github.com/scaleway/scaleway-sdk-go/parameter"
"github.com/scaleway/scaleway-sdk-go/scw"
)
// always import dependencies
var (
_ fmt.Stringer
_ json.Unmarshaler
_ url.URL
_ net.IP
_ http.Header
_ bytes.Reader
_ time.Time
_ = strings.Join
_ scw.ScalewayRequest
_ marshaler.Duration
_ scw.File
_ = parameter.AddToQuery
_ = namegenerator.GetRandomName
)
type DataKeyAlgorithmSymmetricEncryption string
const (
DataKeyAlgorithmSymmetricEncryptionUnknownSymmetricEncryption = DataKeyAlgorithmSymmetricEncryption("unknown_symmetric_encryption")
// AES-GCM (256-bits) is the only data key algorithm currently supported by Key Manager.
DataKeyAlgorithmSymmetricEncryptionAes256Gcm = DataKeyAlgorithmSymmetricEncryption("aes_256_gcm")
)
func (enum DataKeyAlgorithmSymmetricEncryption) String() string {
if enum == "" {
// return default value if empty
return "unknown_symmetric_encryption"
}
return string(enum)
}
func (enum DataKeyAlgorithmSymmetricEncryption) Values() []DataKeyAlgorithmSymmetricEncryption {
return []DataKeyAlgorithmSymmetricEncryption{
"unknown_symmetric_encryption",
"aes_256_gcm",
}
}
func (enum DataKeyAlgorithmSymmetricEncryption) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
}
func (enum *DataKeyAlgorithmSymmetricEncryption) UnmarshalJSON(data []byte) error {
tmp := ""
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*enum = DataKeyAlgorithmSymmetricEncryption(DataKeyAlgorithmSymmetricEncryption(tmp).String())
return nil
}
type KeyAlgorithmSymmetricEncryption string
const (
KeyAlgorithmSymmetricEncryptionUnknownSymmetricEncryption = KeyAlgorithmSymmetricEncryption("unknown_symmetric_encryption")
// AES-GCM (256-bits) is the only key algorithm currently supported by Key Manager.
KeyAlgorithmSymmetricEncryptionAes256Gcm = KeyAlgorithmSymmetricEncryption("aes_256_gcm")
)
func (enum KeyAlgorithmSymmetricEncryption) String() string {
if enum == "" {
// return default value if empty
return "unknown_symmetric_encryption"
}
return string(enum)
}
func (enum KeyAlgorithmSymmetricEncryption) Values() []KeyAlgorithmSymmetricEncryption {
return []KeyAlgorithmSymmetricEncryption{
"unknown_symmetric_encryption",
"aes_256_gcm",
}
}
func (enum KeyAlgorithmSymmetricEncryption) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
}
func (enum *KeyAlgorithmSymmetricEncryption) UnmarshalJSON(data []byte) error {
tmp := ""
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*enum = KeyAlgorithmSymmetricEncryption(KeyAlgorithmSymmetricEncryption(tmp).String())
return nil
}
type KeyOrigin string
const (
KeyOriginUnknownOrigin = KeyOrigin("unknown_origin")
// Scaleway Key Manager generates the key material upon key creation.
KeyOriginScalewayKms = KeyOrigin("scaleway_kms")
// Scaleway Key Manager creates a key with key material coming from an external source.
KeyOriginExternal = KeyOrigin("external")
)
func (enum KeyOrigin) String() string {
if enum == "" {
// return default value if empty
return "unknown_origin"
}
return string(enum)
}
func (enum KeyOrigin) Values() []KeyOrigin {
return []KeyOrigin{
"unknown_origin",
"scaleway_kms",
"external",
}
}
func (enum KeyOrigin) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
}
func (enum *KeyOrigin) UnmarshalJSON(data []byte) error {
tmp := ""
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*enum = KeyOrigin(KeyOrigin(tmp).String())
return nil
}
type KeyState string
const (
KeyStateUnknownState = KeyState("unknown_state")
// The key can be used for cryptographic operations.
KeyStateEnabled = KeyState("enabled")
// The key cannot be used for cryptographic operations.
KeyStateDisabled = KeyState("disabled")
// Key material must be imported before the key can be used for cryptographic operations.
KeyStatePendingKeyMaterial = KeyState("pending_key_material")
)
func (enum KeyState) String() string {
if enum == "" {
// return default value if empty
return "unknown_state"
}
return string(enum)
}
func (enum KeyState) Values() []KeyState {
return []KeyState{
"unknown_state",
"enabled",
"disabled",
"pending_key_material",
}
}
func (enum KeyState) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
}
func (enum *KeyState) UnmarshalJSON(data []byte) error {
tmp := ""
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*enum = KeyState(KeyState(tmp).String())
return nil
}
type ListKeysRequestOrderBy string
const (
ListKeysRequestOrderByNameAsc = ListKeysRequestOrderBy("name_asc")
ListKeysRequestOrderByNameDesc = ListKeysRequestOrderBy("name_desc")
ListKeysRequestOrderByCreatedAtAsc = ListKeysRequestOrderBy("created_at_asc")
ListKeysRequestOrderByCreatedAtDesc = ListKeysRequestOrderBy("created_at_desc")
ListKeysRequestOrderByUpdatedAtAsc = ListKeysRequestOrderBy("updated_at_asc")
ListKeysRequestOrderByUpdatedAtDesc = ListKeysRequestOrderBy("updated_at_desc")
)
func (enum ListKeysRequestOrderBy) String() string {
if enum == "" {
// return default value if empty
return "name_asc"
}
return string(enum)
}
func (enum ListKeysRequestOrderBy) Values() []ListKeysRequestOrderBy {
return []ListKeysRequestOrderBy{
"name_asc",
"name_desc",
"created_at_asc",
"created_at_desc",
"updated_at_asc",
"updated_at_desc",
}
}
func (enum ListKeysRequestOrderBy) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
}
func (enum *ListKeysRequestOrderBy) UnmarshalJSON(data []byte) error {
tmp := ""
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*enum = ListKeysRequestOrderBy(ListKeysRequestOrderBy(tmp).String())
return nil
}
// KeyRotationPolicy: key rotation policy.
type KeyRotationPolicy struct {
// RotationPeriod: duration between two key rotations. The minimum duration is 24 hours and the maximum duration is 876000 hours (1 year).
RotationPeriod *scw.Duration `json:"rotation_period"`
// NextRotationAt: date at which the key will be rotated next.
NextRotationAt *time.Time `json:"next_rotation_at"`
}
// KeyUsage: key usage.
type KeyUsage struct {
// SymmetricEncryption: see the `Key.Algorithm.SymmetricEncryption` enum for a description of values.
// Default value: unknown_symmetric_encryption
// Precisely one of SymmetricEncryption must be set.
SymmetricEncryption *KeyAlgorithmSymmetricEncryption `json:"symmetric_encryption,omitempty"`
}
// Key: key.
type Key struct {
// ID: ID of the key.
ID string `json:"id"`
// ProjectID: ID of the Project containing the key.
ProjectID string `json:"project_id"`
// Name: name of the key.
Name string `json:"name"`
// Usage: keys with a usage set to `symmetric_encryption` are used to encrypt and decrypt data. The only key algorithm currently supported by Key Manager is AES-256-GCM.
Usage *KeyUsage `json:"usage"`
// State: see the `Key.State` enum for a description of values.
// Default value: unknown_state
State KeyState `json:"state"`
// RotationCount: the rotation count tracks the amount of times that the key was rotated.
RotationCount uint32 `json:"rotation_count"`
// CreatedAt: key creation date.
CreatedAt *time.Time `json:"created_at"`
// UpdatedAt: key last modification date.
UpdatedAt *time.Time `json:"updated_at"`
// Protected: returns `true` if key protection is applied to the key.
Protected bool `json:"protected"`
// Locked: returns `true` if the key is locked.
Locked bool `json:"locked"`
// Description: description of the key.
Description *string `json:"description"`
// Tags: list of the key's tags.
Tags []string `json:"tags"`
// RotatedAt: key last rotation date.
RotatedAt *time.Time `json:"rotated_at"`
// RotationPolicy: key rotation policy.
RotationPolicy *KeyRotationPolicy `json:"rotation_policy"`
// Origin: refer to the `Key.Origin` enum for a description of values.
// Default value: unknown_origin
Origin KeyOrigin `json:"origin"`
// Region: region of the key.
Region scw.Region `json:"region"`
}
// CreateKeyRequest: create key request.
type CreateKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// ProjectID: ID of the Project containing the key.
ProjectID string `json:"project_id"`
// Name: (Optional) Name of the key.
Name *string `json:"name,omitempty"`
// Usage: see the `Key.Algorithm.SymmetricEncryption` enum for a description of values.
Usage *KeyUsage `json:"usage,omitempty"`
// Description: (Optional) Description of the key.
Description *string `json:"description,omitempty"`
// Tags: (Optional) List of the key's tags.
Tags []string `json:"tags"`
// RotationPolicy: if not specified, no rotation policy will be applied to the key.
RotationPolicy *KeyRotationPolicy `json:"rotation_policy,omitempty"`
// Unprotected: default value is `false`.
Unprotected bool `json:"unprotected"`
// Origin: refer to the `Key.Origin` enum for a description of values.
// Default value: unknown_origin
Origin KeyOrigin `json:"origin"`
}
// DataKey: data key.
type DataKey struct {
// KeyID: ID of the data encryption key.
KeyID string `json:"key_id"`
// Algorithm: symmetric encryption algorithm of the data encryption key.
// Default value: unknown_symmetric_encryption
Algorithm DataKeyAlgorithmSymmetricEncryption `json:"algorithm"`
// Ciphertext: your data encryption key's ciphertext can be stored safely. It can only be decrypted through the keys you create in Key Manager, using the relevant key ID.
Ciphertext []byte `json:"ciphertext"`
// Plaintext: (Optional) Your data encryption key's plaintext allows you to use the key immediately upon creation. It must neither be stored or shared.
Plaintext *[]byte `json:"plaintext"`
// CreatedAt: data encryption key creation date.
CreatedAt *time.Time `json:"created_at"`
}
// DecryptRequest: decrypt request.
type DecryptRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to decrypt.
KeyID string `json:"-"`
// Ciphertext: data size must be between 1 and 131071 bytes.
Ciphertext []byte `json:"ciphertext"`
// AssociatedData: the additional data must match the value passed in the encryption request.
AssociatedData *[]byte `json:"associated_data,omitempty"`
}
// DecryptResponse: decrypt response.
type DecryptResponse struct {
// KeyID: ID of the key used for decryption.
KeyID string `json:"key_id"`
// Plaintext: key's decrypted data.
Plaintext []byte `json:"plaintext"`
// Ciphertext: if the data was already encrypted with the latest key rotation, no output will be returned in the response object.
Ciphertext *[]byte `json:"ciphertext"`
}
// DeleteKeyMaterialRequest: delete key material request.
type DeleteKeyMaterialRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key of which to delete the key material.
KeyID string `json:"-"`
}
// DeleteKeyRequest: delete key request.
type DeleteKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to delete.
KeyID string `json:"-"`
}
// DisableKeyRequest: disable key request.
type DisableKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to disable.
KeyID string `json:"-"`
}
// EnableKeyRequest: enable key request.
type EnableKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to enable.
KeyID string `json:"-"`
}
// EncryptRequest: encrypt request.
type EncryptRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to encrypt.
KeyID string `json:"-"`
// Plaintext: data size must be between 1 and 65535 bytes.
Plaintext []byte `json:"plaintext"`
// AssociatedData: additional data which will not be encrypted, but authenticated and appended to the encrypted payload.
AssociatedData *[]byte `json:"associated_data,omitempty"`
}
// EncryptResponse: encrypt response.
type EncryptResponse struct {
// KeyID: ID of the key used for encryption.
KeyID string `json:"key_id"`
// Ciphertext: key's encrypted data.
Ciphertext []byte `json:"ciphertext"`
}
// GenerateDataKeyRequest: generate data key request.
type GenerateDataKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key.
KeyID string `json:"-"`
// Algorithm: see the `DataKey.Algorithm.SymmetricEncryption` enum for a description of values.
// Default value: unknown_symmetric_encryption
Algorithm DataKeyAlgorithmSymmetricEncryption `json:"algorithm"`
// WithoutPlaintext: default value is `false`, meaning that the plaintext is returned.
// Set it to `true` if you do not wish the plaintext to be returned in the response object.
WithoutPlaintext bool `json:"without_plaintext"`
}
// GetKeyRequest: get key request.
type GetKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to target.
KeyID string `json:"-"`
}
// ImportKeyMaterialRequest: import key material request.
type ImportKeyMaterialRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: the key's origin must be 'external'.
KeyID string `json:"-"`
// KeyMaterial: the key material The key material is a random sequence of bytes used to derive a cryptographic key.
KeyMaterial []byte `json:"key_material"`
// Salt: a salt can be used to improve the quality of randomness when the key material is generated from a low entropy source.
Salt *[]byte `json:"salt,omitempty"`
}
// ListKeysRequest: list keys request.
type ListKeysRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// OrganizationID: (Optional) Filter by Organization ID.
OrganizationID *string `json:"-"`
// ProjectID: (Optional) Filter by Project ID.
ProjectID *string `json:"-"`
// OrderBy: default value: name_asc
OrderBy ListKeysRequestOrderBy `json:"-"`
Page *int32 `json:"-"`
PageSize *uint32 `json:"-"`
// Tags: (Optional) List of tags to filter on.
Tags []string `json:"-"`
// Name: (Optional) Filter by key name.
Name *string `json:"-"`
}
// ListKeysResponse: list keys response.
type ListKeysResponse struct {
// Keys: single page of keys matching the requested criteria.
Keys []*Key `json:"keys"`
// TotalCount: total count of keys matching the requested criteria.
TotalCount uint64 `json:"total_count"`
}
// UnsafeGetTotalCount should not be used
// Internal usage only
func (r *ListKeysResponse) UnsafeGetTotalCount() uint64 {
return r.TotalCount
}
// UnsafeAppend should not be used
// Internal usage only
func (r *ListKeysResponse) UnsafeAppend(res interface{}) (uint64, error) {
results, ok := res.(*ListKeysResponse)
if !ok {
return 0, errors.New("%T type cannot be appended to type %T", res, r)
}
r.Keys = append(r.Keys, results.Keys...)
r.TotalCount += uint64(len(results.Keys))
return uint64(len(results.Keys)), nil
}
// ProtectKeyRequest: protect key request.
type ProtectKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to apply key protection to.
KeyID string `json:"-"`
}
// RotateKeyRequest: rotate key request.
type RotateKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to rotate.
KeyID string `json:"-"`
}
// UnprotectKeyRequest: unprotect key request.
type UnprotectKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to remove key protection from.
KeyID string `json:"-"`
}
// UpdateKeyRequest: update key request.
type UpdateKeyRequest struct {
// Region: region to target. If none is passed will use default region from the config.
Region scw.Region `json:"-"`
// KeyID: ID of the key to update.
KeyID string `json:"-"`
// Name: (Optional) Updated name of the key.
Name *string `json:"name,omitempty"`
// Description: (Optional) Updated description of the key.
Description *string `json:"description,omitempty"`
// Tags: (Optional) Updated list of the key's tags.
Tags *[]string `json:"tags,omitempty"`
// RotationPolicy: if not specified, the key's existing rotation policy applies.
RotationPolicy *KeyRotationPolicy `json:"rotation_policy,omitempty"`
}
// This API allows you to create, manage and use cryptographic keys in a centralized and secure service.
type API struct {
client *scw.Client
}
// NewAPI returns a API object from a Scaleway client.
func NewAPI(client *scw.Client) *API {
return &API{
client: client,
}
}
func (s *API) Regions() []scw.Region {
return []scw.Region{scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw}
}
// CreateKey: Create a key in a given region specified by the `region` parameter. Keys only support symmetric encryption. You can use keys to encrypt or decrypt arbitrary payloads, or to generate data encryption keys that can be used without being stored in Key Manager.
func (s *API) CreateKey(req *CreateKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if req.ProjectID == "" {
defaultProjectID, _ := s.client.GetDefaultProjectID()
req.ProjectID = defaultProjectID
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// GetKey: Retrieve the metadata of a key specified by the `region` and `key_id` parameters.
func (s *API) GetKey(req *GetKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "GET",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "",
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// UpdateKey: Update a key's metadata (name, description and tags), specified by the `key_id` and `region` parameters.
func (s *API) UpdateKey(req *UpdateKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "PATCH",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// DeleteKey: Delete an existing key specified by the `region` and `key_id` parameters. Deleting a key is permanent and cannot be undone. All data encrypted using this key, including data encryption keys, will become unusable.
func (s *API) DeleteKey(req *DeleteKeyRequest, opts ...scw.RequestOption) error {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "DELETE",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "",
}
err = s.client.Do(scwReq, nil, opts...)
if err != nil {
return err
}
return nil
}
// RotateKey: Generate a new version of an existing key with randomly generated key material. Rotated keys can still be used to decrypt previously encrypted data. The key's new material will be used for subsequent encryption operations and data key generation.
func (s *API) RotateKey(req *RotateKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/rotate",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// ProtectKey: Apply key protection to a given key specified by the `key_id` parameter. Applying key protection means that your key can be used and modified, but it cannot be deleted.
func (s *API) ProtectKey(req *ProtectKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/protect",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// UnprotectKey: Remove key protection from a given key specified by the `key_id` parameter. Removing key protection means that your key can be deleted anytime.
func (s *API) UnprotectKey(req *UnprotectKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/unprotect",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// EnableKey: Enable a given key to be used for cryptographic operations. Enabling a key allows you to make a disabled key usable again. You must specify the `region` and `key_id` parameters.
func (s *API) EnableKey(req *EnableKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/enable",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// DisableKey: Disable a given key to be used for cryptographic operations. Disabling a key renders it unusable. You must specify the `region` and `key_id` parameters.
func (s *API) DisableKey(req *DisableKeyRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/disable",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// ListKeys: Retrieve the list of keys created within all Projects of an Organization or in a given Project. You must specify the `region`, and either the `organization_id` or the `project_id`.
func (s *API) ListKeys(req *ListKeysRequest, opts ...scw.RequestOption) (*ListKeysResponse, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
defaultPageSize, exist := s.client.GetDefaultPageSize()
if (req.PageSize == nil || *req.PageSize == 0) && exist {
req.PageSize = &defaultPageSize
}
query := url.Values{}
parameter.AddToQuery(query, "organization_id", req.OrganizationID)
parameter.AddToQuery(query, "project_id", req.ProjectID)
parameter.AddToQuery(query, "order_by", req.OrderBy)
parameter.AddToQuery(query, "page", req.Page)
parameter.AddToQuery(query, "page_size", req.PageSize)
parameter.AddToQuery(query, "tags", req.Tags)
parameter.AddToQuery(query, "name", req.Name)
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "GET",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys",
Query: query,
}
var resp ListKeysResponse
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// GenerateDataKey: Generate a new data encryption key to use for cryptographic operations outside of Key Manager. Note that Key Manager does not store your data encryption key. The data encryption key is encrypted and must be decrypted using the key you have created in Key Manager. The data encryption key's plaintext is returned in the response object, for immediate usage.
//
// Always store the data encryption key's ciphertext, rather than its plaintext, which must not be stored. To retrieve your key's plaintext, call the Decrypt endpoint with your key's ID and ciphertext.
func (s *API) GenerateDataKey(req *GenerateDataKeyRequest, opts ...scw.RequestOption) (*DataKey, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/generate-data-key",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp DataKey
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// Encrypt: Encrypt data using an existing key, specified by the `key_id` parameter. Only keys with a usage set to **symmetric_encryption** are supported by this method. The maximum payload size that can be encrypted is 64KB of plaintext.
func (s *API) Encrypt(req *EncryptRequest, opts ...scw.RequestOption) (*EncryptResponse, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/encrypt",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp EncryptResponse
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// Decrypt: Decrypt data using an existing key, specified by the `key_id` parameter. The maximum payload size that can be decrypted is the result of the encryption of 64KB of data (around 131KB).
func (s *API) Decrypt(req *DecryptRequest, opts ...scw.RequestOption) (*DecryptResponse, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/decrypt",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp DecryptResponse
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// ImportKeyMaterial: Import key material to use to derive a new cryptographic key. The key's origin must be `external`.
func (s *API) ImportKeyMaterial(req *ImportKeyMaterialRequest, opts ...scw.RequestOption) (*Key, error) {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return nil, errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return nil, errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/import-key-material",
}
err = scwReq.SetBody(req)
if err != nil {
return nil, err
}
var resp Key
err = s.client.Do(scwReq, &resp, opts...)
if err != nil {
return nil, err
}
return &resp, nil
}
// DeleteKeyMaterial: Delete previously imported key material. This renders the associated cryptographic key unusable for any operation. The key's origin must be `external`.
func (s *API) DeleteKeyMaterial(req *DeleteKeyMaterialRequest, opts ...scw.RequestOption) error {
var err error
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}
if fmt.Sprint(req.Region) == "" {
return errors.New("field Region cannot be empty in request")
}
if fmt.Sprint(req.KeyID) == "" {
return errors.New("field KeyID cannot be empty in request")
}
scwReq := &scw.ScalewayRequest{
Method: "POST",
Path: "/key-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/keys/" + fmt.Sprint(req.KeyID) + "/delete-key-material",
}
err = scwReq.SetBody(req)
if err != nil {
return err
}
err = s.client.Do(scwReq, nil, opts...)
if err != nil {
return err
}
return nil
}
|