Skip to content

Commit fa0c05f

Browse files
authored
ovh: allow to use ovh.conf file (go-acme#2216)
1 parent 4cbe9a2 commit fa0c05f

File tree

2 files changed

+43
-129
lines changed

2 files changed

+43
-129
lines changed

providers/dns/ovh/ovh.go

Lines changed: 29 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// OVH API reference: https://eu.api.ovh.com/
1717
// Create a Token: https://eu.api.ovh.com/createToken/
18-
// Create a OAuth2 client: https://eu.api.ovh.com/console-preview/?section=%2Fme&branch=v1#post-/me/api/oauth2/client
18+
// Create a OAuth2 client: https://eu.api.ovh.com/console/?section=%2Fme&branch=v1#post-/me/api/oauth2/client
1919

2020
// Environment variables names.
2121
const (
@@ -102,9 +102,23 @@ type DNSProvider struct {
102102
// Credentials must be passed in the environment variables:
103103
// OVH_ENDPOINT (must be either "ovh-eu" or "ovh-ca"), OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET, OVH_CONSUMER_KEY.
104104
func NewDNSProvider() (*DNSProvider, error) {
105-
config, err := createConfigFromEnvVars()
106-
if err != nil {
107-
return nil, fmt.Errorf("ovh: %w", err)
105+
config := NewDefaultConfig()
106+
107+
// https://github.com/ovh/go-ovh/blob/6817886d12a8c5650794b28da635af9fcdfd1162/ovh/configuration.go#L105
108+
config.APIEndpoint = env.GetOrDefaultString(EnvEndpoint, "ovh-eu")
109+
110+
config.ApplicationKey = env.GetOrFile(EnvApplicationKey)
111+
config.ApplicationSecret = env.GetOrFile(EnvApplicationSecret)
112+
config.ConsumerKey = env.GetOrFile(EnvConsumerKey)
113+
114+
clientID := env.GetOrFile(EnvClientID)
115+
clientSecret := env.GetOrFile(EnvClientSecret)
116+
117+
if clientID != "" || clientSecret != "" {
118+
config.OAuth2Config = &OAuth2Config{
119+
ClientID: clientID,
120+
ClientSecret: clientSecret,
121+
}
108122
}
109123

110124
return NewDNSProviderConfig(config)
@@ -125,8 +139,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
125139
return nil, fmt.Errorf("ovh: %w", err)
126140
}
127141

128-
client.Client = config.HTTPClient
129-
130142
return &DNSProvider{
131143
config: config,
132144
client: client,
@@ -222,94 +234,24 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
222234
return d.config.PropagationTimeout, d.config.PollingInterval
223235
}
224236

225-
func createConfigFromEnvVars() (*Config, error) {
226-
firstAppKeyEnvVar := findFirstValuedEnvVar(EnvApplicationKey, EnvApplicationSecret, EnvConsumerKey)
227-
firstOAuth2EnvVar := findFirstValuedEnvVar(EnvClientID, EnvClientSecret)
228-
229-
if firstAppKeyEnvVar != "" && firstOAuth2EnvVar != "" {
230-
return nil, fmt.Errorf("can't use both %s and %s at the same time", firstAppKeyEnvVar, firstOAuth2EnvVar)
231-
}
232-
233-
config := NewDefaultConfig()
234-
235-
if firstOAuth2EnvVar != "" {
236-
values, err := env.Get(EnvEndpoint, EnvClientID, EnvClientSecret)
237-
if err != nil {
238-
return nil, err
239-
}
240-
241-
config.APIEndpoint = values[EnvEndpoint]
242-
config.OAuth2Config = &OAuth2Config{
243-
ClientID: values[EnvClientID],
244-
ClientSecret: values[EnvClientSecret],
245-
}
246-
247-
return config, nil
248-
}
249-
250-
values, err := env.Get(EnvEndpoint, EnvApplicationKey, EnvApplicationSecret, EnvConsumerKey)
251-
if err != nil {
252-
return nil, err
253-
}
254-
255-
config.APIEndpoint = values[EnvEndpoint]
256-
257-
config.ApplicationKey = values[EnvApplicationKey]
258-
config.ApplicationSecret = values[EnvApplicationSecret]
259-
config.ConsumerKey = values[EnvConsumerKey]
260-
261-
return config, nil
262-
}
263-
264-
func findFirstValuedEnvVar(envVars ...string) string {
265-
for _, envVar := range envVars {
266-
if env.GetOrFile(envVar) != "" {
267-
return envVar
268-
}
269-
}
270-
271-
return ""
272-
}
273-
274237
func newClient(config *Config) (*ovh.Client, error) {
275-
if config.OAuth2Config == nil {
276-
return newClientApplicationKey(config)
238+
var client *ovh.Client
239+
var err error
240+
241+
switch {
242+
case config.hasAppKeyAuth():
243+
client, err = ovh.NewClient(config.APIEndpoint, config.ApplicationKey, config.ApplicationSecret, config.ConsumerKey)
244+
case config.OAuth2Config != nil:
245+
client, err = ovh.NewOAuth2Client(config.APIEndpoint, config.OAuth2Config.ClientID, config.OAuth2Config.ClientSecret)
246+
default:
247+
client, err = ovh.NewDefaultClient()
277248
}
278249

279-
return newClientOAuth2(config)
280-
}
281-
282-
func newClientApplicationKey(config *Config) (*ovh.Client, error) {
283-
if config.APIEndpoint == "" || config.ApplicationKey == "" || config.ApplicationSecret == "" || config.ConsumerKey == "" {
284-
return nil, errors.New("credentials are missing")
285-
}
286-
287-
client, err := ovh.NewClient(
288-
config.APIEndpoint,
289-
config.ApplicationKey,
290-
config.ApplicationSecret,
291-
config.ConsumerKey,
292-
)
293250
if err != nil {
294251
return nil, fmt.Errorf("new client: %w", err)
295252
}
296253

297-
return client, nil
298-
}
299-
300-
func newClientOAuth2(config *Config) (*ovh.Client, error) {
301-
if config.APIEndpoint == "" || config.OAuth2Config.ClientID == "" || config.OAuth2Config.ClientSecret == "" {
302-
return nil, errors.New("credentials are missing")
303-
}
304-
305-
client, err := ovh.NewOAuth2Client(
306-
config.APIEndpoint,
307-
config.OAuth2Config.ClientID,
308-
config.OAuth2Config.ClientSecret,
309-
)
310-
if err != nil {
311-
return nil, fmt.Errorf("new OAuth2 client: %w", err)
312-
}
254+
client.UserAgent = "go-acme/lego"
313255

314256
return client, nil
315257
}

providers/dns/ovh/ovh_test.go

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ func TestNewDNSProvider(t *testing.T) {
3434
EnvConsumerKey: "D",
3535
},
3636
},
37-
{
38-
desc: "application key: missing endpoint",
39-
envVars: map[string]string{
40-
EnvEndpoint: "",
41-
EnvApplicationKey: "B",
42-
EnvApplicationSecret: "C",
43-
EnvConsumerKey: "D",
44-
},
45-
expected: "ovh: some credentials information are missing: OVH_ENDPOINT",
46-
},
4737
{
4838
desc: "application key: missing invalid endpoint",
4939
envVars: map[string]string{
@@ -62,7 +52,7 @@ func TestNewDNSProvider(t *testing.T) {
6252
EnvApplicationSecret: "C",
6353
EnvConsumerKey: "D",
6454
},
65-
expected: "ovh: some credentials information are missing: OVH_APPLICATION_KEY",
55+
expected: "ovh: new client: invalid authentication config, both application_key and application_secret must be given",
6656
},
6757
{
6858
desc: "application key: missing application secret",
@@ -72,17 +62,7 @@ func TestNewDNSProvider(t *testing.T) {
7262
EnvApplicationSecret: "",
7363
EnvConsumerKey: "D",
7464
},
75-
expected: "ovh: some credentials information are missing: OVH_APPLICATION_SECRET",
76-
},
77-
{
78-
desc: "application key: missing consumer key",
79-
envVars: map[string]string{
80-
EnvEndpoint: "ovh-eu",
81-
EnvApplicationKey: "B",
82-
EnvApplicationSecret: "C",
83-
EnvConsumerKey: "",
84-
},
85-
expected: "ovh: some credentials information are missing: OVH_CONSUMER_KEY",
65+
expected: "ovh: new client: invalid authentication config, both application_key and application_secret must be given",
8666
},
8767
{
8868
desc: "oauth2: success",
@@ -99,7 +79,7 @@ func TestNewDNSProvider(t *testing.T) {
9979
EnvClientID: "E",
10080
EnvClientSecret: "",
10181
},
102-
expected: "ovh: some credentials information are missing: OVH_CLIENT_SECRET",
82+
expected: "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given",
10383
},
10484
{
10585
desc: "oauth2: missing client ID",
@@ -108,7 +88,7 @@ func TestNewDNSProvider(t *testing.T) {
10888
EnvClientID: "",
10989
EnvClientSecret: "F",
11090
},
111-
expected: "ovh: some credentials information are missing: OVH_CLIENT_ID",
91+
expected: "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given",
11292
},
11393
{
11494
desc: "missing credentials",
@@ -120,7 +100,7 @@ func TestNewDNSProvider(t *testing.T) {
120100
EnvClientID: "",
121101
EnvClientSecret: "",
122102
},
123-
expected: "ovh: some credentials information are missing: OVH_ENDPOINT,OVH_APPLICATION_KEY,OVH_APPLICATION_SECRET,OVH_CONSUMER_KEY",
103+
expected: "ovh: new client: missing authentication information, you need to provide at least an application_key/application_secret or a client_id/client_secret",
124104
},
125105
{
126106
desc: "mixed auth",
@@ -132,7 +112,7 @@ func TestNewDNSProvider(t *testing.T) {
132112
EnvClientID: "E",
133113
EnvClientSecret: "F",
134114
},
135-
expected: "ovh: can't use both OVH_APPLICATION_KEY and OVH_CLIENT_ID at the same time",
115+
expected: "ovh: can't use both authentication systems (ApplicationKey and OAuth2)",
136116
},
137117
}
138118

@@ -182,7 +162,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
182162
applicationKey: "B",
183163
applicationSecret: "C",
184164
consumerKey: "D",
185-
expected: "ovh: credentials are missing",
165+
expected: "ovh: new client: unknown endpoint '', consider checking 'Endpoints' list or using an URL",
186166
},
187167
{
188168
desc: "application key: invalid api endpoint",
@@ -198,23 +178,15 @@ func TestNewDNSProviderConfig(t *testing.T) {
198178
applicationKey: "",
199179
applicationSecret: "C",
200180
consumerKey: "D",
201-
expected: "ovh: credentials are missing",
181+
expected: "ovh: new client: invalid authentication config, both application_key and application_secret must be given",
202182
},
203183
{
204184
desc: "application key: missing application secret",
205185
apiEndpoint: "ovh-eu",
206186
applicationKey: "B",
207187
applicationSecret: "",
208188
consumerKey: "D",
209-
expected: "ovh: credentials are missing",
210-
},
211-
{
212-
desc: "application key: missing consumer key",
213-
apiEndpoint: "ovh-eu",
214-
applicationKey: "B",
215-
applicationSecret: "C",
216-
consumerKey: "",
217-
expected: "ovh: credentials are missing",
189+
expected: "ovh: new client: invalid authentication config, both application_key and application_secret must be given",
218190
},
219191
{
220192
desc: "oauth2: success",
@@ -227,32 +199,32 @@ func TestNewDNSProviderConfig(t *testing.T) {
227199
apiEndpoint: "",
228200
clientID: "B",
229201
clientSecret: "C",
230-
expected: "ovh: credentials are missing",
202+
expected: "ovh: new client: unknown endpoint '', consider checking 'Endpoints' list or using an URL",
231203
},
232204
{
233205
desc: "oauth2: invalid api endpoint",
234206
apiEndpoint: "foobar",
235207
clientID: "B",
236208
clientSecret: "C",
237-
expected: "ovh: new OAuth2 client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL",
209+
expected: "ovh: new client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL",
238210
},
239211
{
240212
desc: "oauth2: missing client id",
241213
apiEndpoint: "ovh-eu",
242214
clientID: "",
243215
clientSecret: "C",
244-
expected: "ovh: credentials are missing",
216+
expected: "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given",
245217
},
246218
{
247219
desc: "oauth2: missing client secret",
248220
apiEndpoint: "ovh-eu",
249221
clientID: "B",
250222
clientSecret: "",
251-
expected: "ovh: credentials are missing",
223+
expected: "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given",
252224
},
253225
{
254226
desc: "missing credentials",
255-
expected: "ovh: credentials are missing",
227+
expected: "ovh: new client: missing authentication information, you need to provide at least an application_key/application_secret or a client_id/client_secret",
256228
},
257229
{
258230
desc: "mixed auth",

0 commit comments

Comments
 (0)