Skip to content

Commit 82f9ac2

Browse files
authored
Added support for API token authentication; (#251)
* Added support for API token authentication; * added documentation about API token usage; * fix * fix * Added check;
1 parent e68108b commit 82f9ac2

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
[![release](https://github.com/Myra-Security-GmbH/terraform-provider-myrasec/actions/workflows/release.yml/badge.svg?branch=v1.49.0)](https://github.com/Myra-Security-GmbH/terraform-provider-myrasec/actions/workflows/release.yml)
77
[![tests](https://github.com/Myra-Security-GmbH/terraform-provider-myrasec/actions/workflows/test.yml/badge.svg)](https://github.com/Myra-Security-GmbH/terraform-provider-myrasec/actions/workflows/test.yml)
88
## Documentation
9-
10-
- [How-to guide](https://github.com/Myra-Security-GmbH/terraform-provider-documentation)
119
- [Official documentation](https://registry.terraform.io/providers/Myra-Security-GmbH/myrasec/latest/docs)
1210

1311
## Requirements

docs/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ provider "myrasec" {
2525
...
2626
```
2727

28+
In case you prefer to use the API token for authentication, you can use this token, too:
29+
```
30+
# Configure the Myra Security Provider
31+
provider "myrasec" {
32+
api_token = "${var.myra_api_token}"
33+
}
34+
```
35+
2836
## Variables
2937
Some attributes in the resources require specific values, therefore we created a list of variables that you can import to your terraform project:
3038
[Variable list](variables.md)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/Myra-Security-GmbH/terraform-provider-myrasec
33
go 1.24.0
44

55
require (
6-
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.48.0
6+
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.50.0
77
github.com/hashicorp/go-multierror v1.1.1
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.0
99
golang.org/x/net v0.47.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.48.0 h1:x9GDyLg47fYT9fst7MD4+nSkre7DeytGlq0Gu60xFjo=
22
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.48.0/go.mod h1:Sb2R2gu+OpcGCqoH5fjFrduyGcmYj5mJTT+/zgV4zDE=
3+
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.50.0 h1:YJP5UFVPFqclQEZkm233tM+XkzBUs7Wh1+GSVoylbFg=
4+
github.com/Myra-Security-GmbH/myrasec-go/v2 v2.50.0/go.mod h1:JNnRAqwOMJ3x7VCAhoN9csnqxki2VkeYCXKGDKMN4nE=
35
github.com/Myra-Security-GmbH/signature v1.1.0 h1:/Tv8SilN0P8k5fKArvQHkf9iJWU5H34TSvgEyyZ32f4=
46
github.com/Myra-Security-GmbH/signature v1.1.0/go.mod h1:kyX4FQ2XWvJQnvxkWmcyUIqG0jAzGL22fQMf2RTvoj0=
57
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=

myrasec/config.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
type Config struct {
1515
APIKey string
1616
Secret string
17+
APIToken string
1718
Language string
1819
APIBaseURL string
1920
APICacheTTL int
@@ -25,10 +26,16 @@ type Config struct {
2526
func (c Config) validate() error {
2627
var err *multierror.Error
2728

28-
if c.APIKey == "" {
29+
hasToken := c.APIToken != ""
30+
31+
if !hasToken && c.APIKey == "" && c.Secret == "" {
32+
err = multierror.Append(err, fmt.Errorf("API token or API Key and API Secret is required for using the Myrasec provider"))
33+
}
34+
35+
if !hasToken && c.APIKey == "" {
2936
err = multierror.Append(err, fmt.Errorf("API Key must be configured for the Myrasec provider"))
3037
}
31-
if c.Secret == "" {
38+
if !hasToken && c.Secret == "" {
3239
err = multierror.Append(err, fmt.Errorf("API Secret must be configured for the Myrasec provider"))
3340
}
3441

@@ -41,7 +48,13 @@ func (c Config) validate() error {
4148

4249
// Client returns a new instance of myrasec API client
4350
func (c Config) Client() (*myrasec.API, error) {
44-
api, err := myrasec.New(c.APIKey, c.Secret)
51+
var api *myrasec.API
52+
var err error
53+
if c.APIToken != "" {
54+
api, err = myrasec.NewWithToken(c.APIToken)
55+
} else {
56+
api, err = myrasec.New(c.APIKey, c.Secret)
57+
}
4558
if err != nil {
4659
return nil, err
4760
}

myrasec/provider.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ func Provider() *schema.Provider {
1313
Schema: map[string]*schema.Schema{
1414
"api_key": {
1515
Type: schema.TypeString,
16-
Required: true,
16+
Required: false,
1717
DefaultFunc: schema.EnvDefaultFunc("MYRASEC_API_KEY", nil),
1818
Description: "Your MYRA API Key",
1919
},
2020
"secret": {
2121
Type: schema.TypeString,
22-
Required: true,
22+
Required: false,
2323
DefaultFunc: schema.EnvDefaultFunc("MYRASEC_API_SECRET", nil),
2424
Description: "Your MYRA API Secret",
2525
},
26+
"api_token": {
27+
Type: schema.TypeString,
28+
Required: false,
29+
DefaultFunc: schema.EnvDefaultFunc("MYRASEC_API_TOKEN", nil),
30+
Description: "Your MYRA API Token",
31+
},
2632
"language": {
2733
Type: schema.TypeString,
2834
Optional: true,
@@ -95,6 +101,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (any, diag.D
95101
config := Config{
96102
APIKey: d.Get("api_key").(string),
97103
Secret: d.Get("secret").(string),
104+
APIToken: d.Get("api_token").(string),
98105
Language: d.Get("language").(string),
99106
APIBaseURL: d.Get("api_base_url").(string),
100107
APICacheTTL: d.Get("api_cache_ttl").(int),

0 commit comments

Comments
 (0)