Skip to content

Commit 81b60aa

Browse files
authored
Merge pull request #668 from 0x416e746f6e/feat/enable-access-token-auth
feat: implement `access_token` auth method
2 parents d5f3841 + dc83c0f commit 81b60aa

File tree

9 files changed

+111
-24
lines changed

9 files changed

+111
-24
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-mux v0.15.0
1313
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
1414
github.com/hashicorp/terraform-plugin-testing v1.7.0
15-
github.com/ovh/go-ovh v1.5.1
15+
github.com/ovh/go-ovh v1.6.0
1616
github.com/ybriffa/rfc3339 v0.0.0-20220203155318-1789e3fd6e70
1717
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819
1818
gopkg.in/yaml.v3 v3.0.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
134134
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
135135
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
136136
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
137-
github.com/ovh/go-ovh v1.5.1 h1:P8O+7H+NQuFK9P/j4sFW5C0fvSS2DnHYGPwdVCp45wI=
138-
github.com/ovh/go-ovh v1.5.1/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC7c=
137+
github.com/ovh/go-ovh v1.6.0 h1:ixLOwxQdzYDx296sXcgS35TOPEahJkpjMGtzPadCjQI=
138+
github.com/ovh/go-ovh v1.6.0/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC7c=
139139
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
140140
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
141141
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

ovh/config.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type Config struct {
1717
Plate string
1818
Endpoint string
1919

20+
// Access token
21+
AccessToken string
22+
2023
// AK / AS / CK authentication information
2124
ApplicationKey string
2225
ApplicationSecret string
@@ -38,13 +41,19 @@ func clientDefault(c *Config) (*ovh.Client, error) {
3841
err error
3942
)
4043

41-
if c.ClientID != "" {
44+
switch {
45+
case c.AccessToken != "":
46+
client, err = ovh.NewAccessTokenClient(
47+
c.Endpoint,
48+
c.AccessToken,
49+
)
50+
case c.ClientID != "":
4251
client, err = ovh.NewOAuth2Client(
4352
c.Endpoint,
4453
c.ClientID,
4554
c.ClientSecret,
4655
)
47-
} else {
56+
default:
4857
client, err = ovh.NewClient(
4958
c.Endpoint,
5059
c.ApplicationKey,

ovh/provider.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ var (
1919
descriptions = map[string]string{
2020
"endpoint": "The OVH API endpoint to target (ex: \"ovh-eu\")",
2121

22-
// Authentication via app key / app secret / comsumer key
22+
// Authentication via short-lived access token
23+
"access_token": "The OVH API Access Token",
24+
25+
// Authentication via app key / app secret / consumer key
2326
"application_key": "The OVH API Application Key",
2427
"application_secret": "The OVH API Application Secret",
2528
"consumer_key": "The OVH API Consumer Key",
@@ -39,6 +42,11 @@ func Provider() *schema.Provider {
3942
Optional: true,
4043
Description: descriptions["endpoint"],
4144
},
45+
"access_token": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: descriptions["access_token"],
49+
},
4250
"application_key": {
4351
Type: schema.TypeString,
4452
Optional: true,
@@ -272,6 +280,9 @@ func ConfigureContextFunc(context context.Context, d *schema.ResourceData) (inte
272280
if v, ok := d.GetOk("endpoint"); ok {
273281
config.Endpoint = v.(string)
274282
}
283+
if v, ok := d.GetOk("access_token"); ok {
284+
config.AccessToken = v.(string)
285+
}
275286
if v, ok := d.GetOk("application_key"); ok {
276287
config.ApplicationKey = v.(string)
277288
}

ovh/provider_new.go

+18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func (p *OvhProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *
3939
Optional: true,
4040
Description: descriptions["endpoint"],
4141
},
42+
"access_token": schema.StringAttribute{
43+
Optional: true,
44+
Description: descriptions["access_token"],
45+
},
4246
"application_key": schema.StringAttribute{
4347
Optional: true,
4448
Description: descriptions["application_key"],
@@ -82,6 +86,16 @@ func (p *OvhProvider) Configure(ctx context.Context, req provider.ConfigureReque
8286
)
8387
}
8488

89+
if config.AccessToken.IsUnknown() {
90+
resp.Diagnostics.AddAttributeError(
91+
path.Root("access_token"),
92+
"Unknown OVH API access_token",
93+
"The provider cannot create the OVH API client as there is a missing or empty value for the API access token."+
94+
"Set the access token value in the configuration or use the OVH_ACCESS_TOKEN environment variable."+
95+
"If either is already set, ensure the value is not empty.",
96+
)
97+
}
98+
8599
if config.ApplicationKey.IsUnknown() {
86100
resp.Diagnostics.AddAttributeError(
87101
path.Root("application_key"),
@@ -144,6 +158,9 @@ func (p *OvhProvider) Configure(ctx context.Context, req provider.ConfigureReque
144158
if !config.Endpoint.IsNull() {
145159
clientConfig.Endpoint = config.Endpoint.ValueString()
146160
}
161+
if !config.AccessToken.IsNull() {
162+
clientConfig.AccessToken = config.AccessToken.ValueString()
163+
}
147164
if !config.ApplicationKey.IsNull() {
148165
clientConfig.ApplicationKey = config.ApplicationKey.ValueString()
149166
}
@@ -202,6 +219,7 @@ func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
202219

203220
type ovhProviderModel struct {
204221
Endpoint types.String `tfsdk:"endpoint"`
222+
AccessToken types.String `tfsdk:"access_token"`
205223
ApplicationKey types.String `tfsdk:"application_key"`
206224
ApplicationSecret types.String `tfsdk:"application_secret"`
207225
ConsumerKey types.String `tfsdk:"consumer_key"`

vendor/github.com/ovh/go-ovh/ovh/configuration.go

+24-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ovh/go-ovh/ovh/ovh.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ github.com/mitchellh/reflectwalk
276276
# github.com/oklog/run v1.0.0
277277
## explicit
278278
github.com/oklog/run
279-
# github.com/ovh/go-ovh v1.5.1
279+
# github.com/ovh/go-ovh v1.6.0
280280
## explicit; go 1.18
281281
github.com/ovh/go-ovh/ovh
282282
# github.com/vmihailenco/msgpack v4.0.4+incompatible

website/docs/index.html.markdown

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ description: |-
88

99
# OVH Provider
1010

11-
The OVH provider is the entry point to interact with the resources provided by OVHcloud.
11+
The OVH provider is the entry point to interact with the resources provided by OVHcloud.
1212

13-
-> __NOTE__ According on your needs, you may need to use additional providers. This [documentation page](https://help.ovhcloud.com/csm/en-gb-terraform-at-ovhcloud?id=kb_article_view&sysparm_article=KB0054612) provides the mapping between the control panel concepts and the terraform providers / ressources.
13+
-> __NOTE__ According on your needs, you may need to use additional providers. This [documentation page](https://help.ovhcloud.com/csm/en-gb-terraform-at-ovhcloud?id=kb_article_view&sysparm_article=KB0054612) provides the mapping between the control panel concepts and the terraform providers / resources.
1414

1515
Use the navigation to the left to read about the available resources.
1616

1717
## Provider configuration
1818

1919
The provider needs to be configured with the proper credentials before it can be used. Requests to OVHcloud APIs require a set of secrets keys and the definition of the API end point. See [First Steps with the API](https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/) (or the French version, [Premiers pas avec les API OVHcloud](https://docs.ovh.com/fr/api/api-premiers-pas/)) for a detailed explanation.
2020

21-
Two forms of authentication are supported by the provider:
22-
- OAuth2, using scopped service accounts, and compatible with OVHcloud IAM
23-
- application key & application secret & consumer key
21+
Three forms of authentication are supported by the provider:
22+
- OAuth2, using scoped service accounts, and compatible with OVHcloud IAM
23+
- Short-lived access token received from
24+
[OVH API](https://support.us.ovhcloud.com/hc/en-us/articles/19901571606547-Using-Service-Accounts-to-Connect-to-OVHcloud-APIs)
25+
(for example with aid of Hashicorp Vault OAuth2 secret engine configured to
26+
work with OVH auth api).
27+
- Application key & application secret & consumer key
2428

2529
### OAuth2
2630

@@ -51,7 +55,7 @@ Alternatively it is suggested to use configuration files or environment
5155
variables so that the same code may run seamlessly in multiple environments.
5256
Production and development for instance.
5357

54-
The provider will first look for direct instanciation parameters then
58+
The provider will first look for direct instantiation parameters then
5559
``OVH_ENDPOINT``, ``OVH_CLIENT_ID`` and ``OVH_CLIENT_SECRET`` environment variables.
5660
If either of these parameter is not provided, it will look for a configuration file of the form:
5761

@@ -83,10 +87,18 @@ project or user.
8387

8488
You can find more details about the configuration parsing on repository [go-ovh](https://github.com/ovh/go-ovh).
8589

90+
### Access token
91+
92+
The provider will look for the token either at ``OVH_ACCESS_TOKEN`` environment
93+
variable, or get it via ``access_token`` argument in the provider's stanza.
94+
95+
Similarly to OAuth2 method, the endpoint must be configured (either via
96+
``endpoint`` argument, or with ``OVH_ENDPOINT`` environment variable).
97+
8698
### Application Key/Application Secret
8799

88100
The required keys are the `application_key`, the `application_secret`, and the `consumer_key`.
89-
These keys can be generated via the [OVHcloud token generation page](https://api.ovh.com/createToken/?GET=/*&POST=/*&PUT=/*&DELETE=/*).
101+
These keys can be generated via the [OVHcloud token generation page](https://api.ovh.com/createToken/?GET=/*&POST=/*&PUT=/*&DELETE=/*).
90102

91103
These parameters can be configured directly in the provider block as shown hereafter.
92104

@@ -113,7 +125,7 @@ Alternatively it is suggested to use configuration files or environment
113125
variables so that the same code may run seamlessly in multiple environments.
114126
Production and development for instance.
115127

116-
The provider will first look for direct instanciation parameters then
128+
The provider will first look for direct instantiation parameters then
117129
``OVH_ENDPOINT``, ``OVH_APPLICATION_KEY``, ``OVH_APPLICATION_SECRET`` and
118130
``OVH_CONSUMER_KEY`` environment variables. If either of these parameter is not
119131
provided, it will look for a configuration file of the form:
@@ -246,12 +258,12 @@ variables must also be set:
246258

247259
* `OVH_CLOUD_PROJECT_FAILOVER_IP_ROUTED_TO_1_TEST` - The GUID of an instance to which failover IP addresses can be attached
248260

249-
* `OVH_CLOUD_PROJECT_FAILOVER_IP_ROUTED_TO_2_TEST` - The GUID of a secondary instance to which failover IP addresses can be attached. There must be 2 as associations can only be updated not removed. To test effectively, the failover ip address must be moved between instances
261+
* `OVH_CLOUD_PROJECT_FAILOVER_IP_ROUTED_TO_2_TEST` - The GUID of a secondary instance to which failover IP addresses can be attached. There must be 2 as associations can only be updated not removed. To test effectively, the failover ip address must be moved between instances
250262

251263
* `OVH_CLOUD_PROJECT_KUBE_REGION_TEST` - The region of your public cloud kubernetes project.
252264

253265
* `OVH_CLOUD_PROJECT_KUBE_VERSION_TEST` - The version of your public cloud kubernetes project.
254-
* `OVH_CLOUD_PROJECT_KUBE_PREV_VERSION_TEST` - The previous version of your public cloud kubernetes project. This is used to test upgrade.
266+
* `OVH_CLOUD_PROJECT_KUBE_PREV_VERSION_TEST` - The previous version of your public cloud kubernetes project. This is used to test upgrade.
255267

256268
* `OVH_DEDICATED_SERVER` - The name of the dedicated server to test dedicated_server_networking resource.
257269

@@ -260,7 +272,7 @@ variables must also be set:
260272
* `OVH_ZONE_TEST` - The domain you own to test the domain_zone resource.
261273

262274
* `OVH_IP_TEST`, `OVH_IP_BLOCK_TEST`, `OVH_IP_REVERSE_TEST` - The values you have to set for testing ip reverse resources.
263-
275+
264276
* `OVH_IP_MOVE_SERVICE_NAME_TEST` - The value you have to set for testing ip move resources.
265277

266278
* `OVH_DBAAS_LOGS_SERVICE_TEST` - The name of your Dbaas logs service.

0 commit comments

Comments
 (0)