11package main
22
33import (
4+ "os"
45 "testing"
56
67 "github.com/ghodss/yaml"
@@ -15,6 +16,8 @@ import (
1516
1617var _ = yaml .YAMLToJSON
1718
19+ const testHashStaticPasswordEnv = "DEX_FOO_USER_PASSWORD"
20+
1821func TestValidConfiguration (t * testing.T ) {
1922 configuration := Config {
2023 Issuer : "http://127.0.0.1:5556/dex" ,
@@ -212,3 +215,163 @@ logger:
212215 t .Errorf ("got!=want: %s" , diff )
213216 }
214217}
218+
219+ func TestUnmarshalConfigWithEnv (t * testing.T ) {
220+ staticPasswordEnv := os .Getenv (testHashStaticPasswordEnv )
221+ if staticPasswordEnv == "" {
222+ t .Skipf ("test environment variable %q not set, skipping" , testHashStaticPasswordEnv )
223+ }
224+ rawConfig := []byte (`
225+ issuer: http://127.0.0.1:5556/dex
226+ storage:
227+ type: postgres
228+ config:
229+ host: 10.0.0.1
230+ port: 65432
231+ maxOpenConns: 5
232+ maxIdleConns: 3
233+ connMaxLifetime: 30
234+ connectionTimeout: 3
235+ web:
236+ http: 127.0.0.1:5556
237+
238+ frontend:
239+ dir: ./web
240+ extra:
241+ foo: bar
242+
243+ staticClients:
244+ - id: example-app
245+ redirectURIs:
246+ - 'http://127.0.0.1:5555/callback'
247+ name: 'Example App'
248+ secret: ZXhhbXBsZS1hcHAtc2VjcmV0
249+
250+ oauth2:
251+ alwaysShowLoginScreen: true
252+
253+ connectors:
254+ - type: mockCallback
255+ id: mock
256+ name: Example
257+ - type: oidc
258+ id: google
259+ name: Google
260+ config:
261+ issuer: https://accounts.google.com
262+ clientID: foo
263+ clientSecret: bar
264+ redirectURI: http://127.0.0.1:5556/dex/callback/google
265+
266+ enablePasswordDB: true
267+ staticPasswords:
268+ 269+ # bcrypt hash of the string "password"
270+ hash: "$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy"
271+ username: "admin"
272+ userID: "08a8684b-db88-4b73-90a9-3cd1661f5466"
273+ 274+ hashFromEnv: "DEX_FOO_USER_PASSWORD"
275+ username: "foo"
276+ userID: "41331323-6f44-45e6-b3b9-2c4b60c02be5"
277+
278+ expiry:
279+ signingKeys: "7h"
280+ idTokens: "25h"
281+ authRequests: "25h"
282+
283+ logger:
284+ level: "debug"
285+ format: "json"
286+ ` )
287+
288+ want := Config {
289+ Issuer : "http://127.0.0.1:5556/dex" ,
290+ Storage : Storage {
291+ Type : "postgres" ,
292+ Config : & sql.Postgres {
293+ NetworkDB : sql.NetworkDB {
294+ Host : "10.0.0.1" ,
295+ Port : 65432 ,
296+ MaxOpenConns : 5 ,
297+ MaxIdleConns : 3 ,
298+ ConnMaxLifetime : 30 ,
299+ ConnectionTimeout : 3 ,
300+ },
301+ },
302+ },
303+ Web : Web {
304+ HTTP : "127.0.0.1:5556" ,
305+ },
306+ Frontend : server.WebConfig {
307+ Dir : "./web" ,
308+ Extra : map [string ]string {
309+ "foo" : "bar" ,
310+ },
311+ },
312+ StaticClients : []storage.Client {
313+ {
314+ ID : "example-app" ,
315+ Secret : "ZXhhbXBsZS1hcHAtc2VjcmV0" ,
316+ Name : "Example App" ,
317+ RedirectURIs : []string {
318+ "http://127.0.0.1:5555/callback" ,
319+ },
320+ },
321+ },
322+ OAuth2 : OAuth2 {
323+ AlwaysShowLoginScreen : true ,
324+ },
325+ StaticConnectors : []Connector {
326+ {
327+ Type : "mockCallback" ,
328+ ID : "mock" ,
329+ Name : "Example" ,
330+ Config : & mock.CallbackConfig {},
331+ },
332+ {
333+ Type : "oidc" ,
334+ ID : "google" ,
335+ Name : "Google" ,
336+ Config : & oidc.Config {
337+ Issuer : "https://accounts.google.com" ,
338+ ClientID : "foo" ,
339+ ClientSecret : "bar" ,
340+ RedirectURI : "http://127.0.0.1:5556/dex/callback/google" ,
341+ },
342+ },
343+ },
344+ EnablePasswordDB : true ,
345+ StaticPasswords : []password {
346+ {
347+ 348+ Hash : []byte ("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy" ),
349+ Username : "admin" ,
350+ UserID : "08a8684b-db88-4b73-90a9-3cd1661f5466" ,
351+ },
352+ {
353+ 354+ Hash : []byte ("$2a$10$33EMT0cVYVlPy6WAMCLsceLYjWhuHpbz5yuZxu/GAFj03J9Lytjuy" ),
355+ Username : "foo" ,
356+ UserID : "41331323-6f44-45e6-b3b9-2c4b60c02be5" ,
357+ },
358+ },
359+ Expiry : Expiry {
360+ SigningKeys : "7h" ,
361+ IDTokens : "25h" ,
362+ AuthRequests : "25h" ,
363+ },
364+ Logger : Logger {
365+ Level : "debug" ,
366+ Format : "json" ,
367+ },
368+ }
369+
370+ var c Config
371+ if err := yaml .Unmarshal (rawConfig , & c ); err != nil {
372+ t .Fatalf ("failed to decode config: %v" , err )
373+ }
374+ if diff := pretty .Compare (c , want ); diff != "" {
375+ t .Errorf ("got!=want: %s" , diff )
376+ }
377+ }
0 commit comments