-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ASM-4587-Terraform-for-Tokenization (#55)
* checked and tested * version
- Loading branch information
1 parent
272f8f4
commit 5c79052
Showing
9 changed files
with
874 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package akeyless | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/akeylesslabs/akeyless-go/v2" | ||
"github.com/akeylesslabs/terraform-provider-akeyless/akeyless/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceDetokenize() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Decrypts text with a tokenizer data source", | ||
Read: dataSourceDetokenizeRead, | ||
Schema: map[string]*schema.Schema{ | ||
"tokenizer_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The name of the tokenizer to use in the decryption process", | ||
}, | ||
"ciphertext": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Data to be decrypted", | ||
}, | ||
"tweak": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "Base64 encoded tweak for vaultless encryption", | ||
}, | ||
"result": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDetokenizeRead(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
token := *provider.token | ||
|
||
var apiErr akeyless.GenericOpenAPIError | ||
ctx := context.Background() | ||
tokenizerName := d.Get("tokenizer_name").(string) | ||
ciphertext := d.Get("ciphertext").(string) | ||
tweak := d.Get("tweak").(string) | ||
|
||
body := akeyless.Detokenize{ | ||
TokenizerName: tokenizerName, | ||
Ciphertext: ciphertext, | ||
Token: &token, | ||
} | ||
common.GetAkeylessPtr(&body.Tweak, tweak) | ||
|
||
rOut, res, err := client.Detokenize(ctx).Body(body).Execute() | ||
if err != nil { | ||
if errors.As(err, &apiErr) { | ||
if res.StatusCode == http.StatusNotFound { | ||
// The resource was deleted outside of the current Terraform workspace, so invalidate this resource | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("can't detokenize: %v", string(apiErr.Body())) | ||
} | ||
return fmt.Errorf("can't detokenize: %v", err) | ||
} | ||
err = d.Set("result", *rOut.Result) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(tokenizerName) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package akeyless | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/akeylesslabs/akeyless-go/v2" | ||
"github.com/akeylesslabs/terraform-provider-akeyless/akeyless/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceTokenize() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Encrypts text with a tokenizer data source", | ||
Read: dataSourceTokenizeRead, | ||
Schema: map[string]*schema.Schema{ | ||
"tokenizer_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The name of the tokenizer to use in the encryption process", | ||
}, | ||
"plaintext": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Data to be encrypted", | ||
}, | ||
"tweak": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "Base64 encoded tweak for vaultless encryption", | ||
}, | ||
"result": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceTokenizeRead(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
token := *provider.token | ||
|
||
var apiErr akeyless.GenericOpenAPIError | ||
ctx := context.Background() | ||
tokenizerName := d.Get("tokenizer_name").(string) | ||
plaintext := d.Get("plaintext").(string) | ||
tweak := d.Get("tweak").(string) | ||
|
||
body := akeyless.Tokenize{ | ||
TokenizerName: tokenizerName, | ||
Plaintext: plaintext, | ||
Token: &token, | ||
} | ||
common.GetAkeylessPtr(&body.Tweak, tweak) | ||
|
||
rOut, res, err := client.Tokenize(ctx).Body(body).Execute() | ||
if err != nil { | ||
if errors.As(err, &apiErr) { | ||
if res.StatusCode == http.StatusNotFound { | ||
// The resource was deleted outside of the current Terraform workspace, so invalidate this resource | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("can't tokenize: %v", string(apiErr.Body())) | ||
} | ||
return fmt.Errorf("can't tokenize: %v", err) | ||
} | ||
err = d.Set("result", *rOut.Result) | ||
if err != nil { | ||
return err | ||
} | ||
if rOut.Tweak != nil { | ||
err = d.Set("tweak", *rOut.Tweak) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
d.SetId(tokenizerName) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.