Skip to content

Commit 5a200b4

Browse files
authored
feat(executor/ovhapi): add support for OAuth2 authentication (#873)
Signed-off-by: Daria Fortunato <daria.kobtseva@ovhcloud.com>
1 parent 7d56fc4 commit 5a200b4

4 files changed

Lines changed: 98 additions & 42 deletions

File tree

executors/ovhapi/README.md

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@
22

33
## Step to test OVH API
44

5-
Use case: you software need to make call to OVH API.<br>
6-
You will need OVH credentials to make API call. Please follow this tutorial to get all needed keys: <br>
5+
Use case: your software needs to make calls to OVH API.<br>
6+
You will need OVH credentials to make API calls. You can either use app keys authentication or OAuth2.
7+
8+
To use app keys authentication, please follow this tutorial: <br>
79
EN: https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/
810

11+
To use OAuth2, please follow this tutorial: <br>
12+
EN: https://help.ovhcloud.com/csm/en-manage-service-account?id=kb_article_view&sysparm_article=KB0059343
13+
914
## Input
10-
In your yaml file, you can use:
1115

12-
```
13-
- endpoint optional, default value: ovh-eu
14-
- applicationKey optional, if noAuth, otherwise mandatory
15-
- applicationSecret optional, if noAuth, otherwise mandatory
16-
- consumerKey optional, if noAuth, otherwise mandatory
17-
- noAuth optional
18-
- headers optional
19-
- resolve optional
20-
- proxy optional
21-
- tlsRootCA optional
22-
23-
- method optional, default value: GET
24-
- path mandatory, example "/me"
25-
- body optional
26-
- bodyFile optional
27-
```
16+
The following parameters are available:
2817

29-
The first batch of parameters can also be defined inside Venom variables like this
18+
| Parameter | Description | Default Value |
19+
|-------------------|------------------------------------------------------------------|---------------|
20+
| endpoint | Optional | ovh-eu |
21+
| applicationKey | Optional if `noAuth`, mandatory if using app keys authentication | |
22+
| applicationSecret | Optional if `noAuth`, mandatory if using app keys authentication | |
23+
| consumerKey | Optional if `noAuth`, mandatory if using app keys authentication | |
24+
| clientID | Optional if `noAuth`, mandatory if using OAuth2 | |
25+
| clientSecret | Optional if `noAuth`, mandatory if using OAuth2 | |
26+
| noAuth | Optional | |
27+
| headers | Optional | |
28+
| resolve | Optional | |
29+
| proxy | Optional | |
30+
| tlsRootCA | Optional | |
31+
32+
| Parameter | Description | Default Value |
33+
|-----------|-------------|---------------|
34+
| method | Optional | GET |
35+
| path | Mandatory | |
36+
| body | Optional | |
37+
| bodyFile | Optional | |
38+
39+
The first batch of parameters can also be defined inside Venom variables like this:
3040

3141
```yaml
3242
vars:
3343
ovh.endpoint: ovh-eu
3444
ovh.applicationKey: foo
3545
ovh.applicationSecret: foo
46+
ovh.clientID: foo
47+
ovh.clientSecret: foo
3648
ovh.consumerKey: foo
3749
ovh.noAuth: false
3850
ovh.headers:
@@ -48,6 +60,9 @@ vars:
4860
```
4961
5062
## Example of an __ovhapi__ TestSuite
63+
64+
### Using App Keys authentication
65+
5166
```yaml
5267
name: Title of TestSuite
5368
testcases:
@@ -65,24 +80,42 @@ testcases:
6580
assertions:
6681
- result.statuscode ShouldEqual 200
6782
- result.bodyjson.nichandle ShouldContainSubstring MY_NICHANDLE
83+
```
84+
85+
### Using OAuth2
6886
87+
```yaml
88+
name: Title of TestSuite
89+
testcases:
90+
- name: me
91+
steps:
92+
- type: ovhapi
93+
endpoint: 'ovh-eu'
94+
clientID: 'CLIENT_ID'
95+
clientSecret: 'CLIENT_SECRET'
96+
method: GET
97+
path: /me
98+
retry: 3
99+
delay: 2
100+
assertions:
101+
- result.statuscode ShouldEqual 200
102+
- result.bodyjson.nichandle ShouldContainSubstring MY_NICHANDLE
69103
```
70104
71105
## Output
72106
73-
```
74-
result.executor
75-
result.timeseconds
76-
result.statuscode
77-
result.body
78-
result.bodyjson
79-
result.err
80-
```
81-
- result.timeseconds: execution duration
82-
- result.err: if exists, this field contains error
83-
- result.body: body of HTTP response
84-
- result.bodyjson: body of HTTP response if it's a json. You can access json data as result.bodyjson.yourkey for example
85-
- result.statuscode: Status Code of HTTP response
107+
The following output fields are available:
108+
109+
| Field | Description |
110+
|--------------------|--------------------------------------|
111+
| result.executor | |
112+
| result.timeseconds | Execution duration |
113+
| result.statuscode | Status Code of HTTP response |
114+
| result.body | Body of HTTP response |
115+
| result.bodyjson | Body of HTTP response if it's a JSON |
116+
| result.err | Error message if exists |
117+
118+
Note that you can access json data as `result.bodyjson.yourkey` for example.
86119

87120
## Default assertion
88121

executors/ovhapi/ovhapi.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Executor struct {
3838
ApplicationKey string `json:"applicationKey" yaml:"applicationKey"`
3939
ApplicationSecret string `json:"applicationSecret" yaml:"applicationSecret"`
4040
ConsumerKey string `json:"consumerKey" yaml:"consumerKey"`
41+
ClientID string `json:"clientID" yaml:"clientID"`
42+
ClientSecret string `json:"clientSecret" yaml:"clientSecret"`
4143
NoAuth *bool `json:"noAuth" yaml:"noAuth"`
4244
Headers Headers `json:"headers" yaml:"headers"`
4345
Resolve []string `json:"resolve" yaml:"resolve"`
@@ -91,6 +93,12 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
9193
if e.ConsumerKey == "" {
9294
e.ConsumerKey = venom.StringVarFromCtx(ctx, "ovh.consumerKey")
9395
}
96+
if e.ClientID == "" {
97+
e.ClientID = venom.StringVarFromCtx(ctx, "ovh.clientID")
98+
}
99+
if e.ClientSecret == "" {
100+
e.ClientSecret = venom.StringVarFromCtx(ctx, "ovh.clientSecret")
101+
}
94102
if e.NoAuth == nil {
95103
noauth := venom.BoolVarFromCtx(ctx, "ovh.noAuth")
96104
e.NoAuth = &noauth
@@ -118,13 +126,25 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
118126
r := Result{}
119127

120128
start := time.Now()
129+
121130
// prepare ovh api client
122-
client, err := ovh.NewClient(
123-
e.Endpoint,
124-
e.ApplicationKey,
125-
e.ApplicationSecret,
126-
e.ConsumerKey,
127-
)
131+
var client *ovh.Client
132+
var err error
133+
if e.ClientID != "" && e.ClientSecret != "" {
134+
venom.Debug(ctx, "creating OAuth2 client")
135+
client, err = ovh.NewOAuth2Client(
136+
e.Endpoint,
137+
e.ClientID,
138+
e.ClientSecret,
139+
)
140+
} else {
141+
client, err = ovh.NewClient(
142+
e.Endpoint,
143+
e.ApplicationKey,
144+
e.ApplicationSecret,
145+
e.ConsumerKey,
146+
)
147+
}
128148
if err != nil {
129149
return nil, err
130150
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/mitchellh/go-homedir v1.1.0
3232
github.com/mitchellh/mapstructure v1.5.0
3333
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b
34-
github.com/ovh/go-ovh v1.4.3
34+
github.com/ovh/go-ovh v1.9.0
3535
github.com/pkg/errors v0.9.1
3636
github.com/rockbears/yaml v0.4.0
3737
github.com/rubenv/sql-migrate v1.5.2
@@ -66,6 +66,7 @@ require (
6666
go.opentelemetry.io/otel/metric v1.35.0 // indirect
6767
go.uber.org/multierr v1.11.0 // indirect
6868
go.uber.org/zap v1.27.0 // indirect
69+
golang.org/x/oauth2 v0.26.0 // indirect
6970
)
7071

7172
require (

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,8 @@ github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt
10241024
github.com/mxk/go-imap v0.0.0-20150429134902-531c36c3f12d h1:+DgqA2tuWi/8VU+gVgBAa7+WZrnFbPKhQWbKBB54cVs=
10251025
github.com/mxk/go-imap v0.0.0-20150429134902-531c36c3f12d/go.mod h1:xacC5qXZnL/ooiitVoe3BtI1OotFTqi5zICBs9J5Fyk=
10261026
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
1027-
github.com/ovh/go-ovh v1.4.3 h1:Gs3V823zwTFpzgGLZNI6ILS4rmxZgJwJCz54Er9LwD0=
1028-
github.com/ovh/go-ovh v1.4.3/go.mod h1:AkPXVtgwB6xlKblMjRKJJmjRp+ogrE7fz2lVgcQY8SY=
1027+
github.com/ovh/go-ovh v1.9.0 h1:6K8VoL3BYjVV3In9tPJUdT7qMx9h0GExN9EXx1r2kKE=
1028+
github.com/ovh/go-ovh v1.9.0/go.mod h1:cTVDnl94z4tl8pP1uZ/8jlVxntjSIf09bNcQ5TJSC7c=
10291029
github.com/paulmach/orb v0.11.1 h1:3koVegMC4X/WeiXYz9iswopaTwMem53NzTJuTF20JzU=
10301030
github.com/paulmach/orb v0.11.1/go.mod h1:5mULz1xQfs3bmQm63QEJA6lNGujuRafwA5S/EnuLaLU=
10311031
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
@@ -1347,6 +1347,8 @@ golang.org/x/oauth2 v0.4.0/go.mod h1:RznEsdpjGAINPTOF0UH/t+xJ75L18YO3Ho6Pyn+uRec
13471347
golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I=
13481348
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
13491349
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=
1350+
golang.org/x/oauth2 v0.26.0 h1:afQXWNNaeC4nvZ0Ed9XvCCzXM6UHJG7iCg0W4fPqSBE=
1351+
golang.org/x/oauth2 v0.26.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
13501352
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13511353
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13521354
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 commit comments

Comments
 (0)