@@ -42,21 +42,7 @@ func newDexClient(hostAndPort, caPath, clientCrt, clientKey string) (api.DexClie
42
42
return api .NewDexClient (conn ), nil
43
43
}
44
44
45
- func main () {
46
- caCrt := flag .String ("ca-crt" , "" , "CA certificate" )
47
- clientCrt := flag .String ("client-crt" , "" , "Client certificate" )
48
- clientKey := flag .String ("client-key" , "" , "Client key" )
49
- flag .Parse ()
50
-
51
- if * clientCrt == "" || * caCrt == "" || * clientKey == "" {
52
- log .Fatal ("Please provide CA & client certificates and client key. Usage: ./client --ca-crt=<path ca.crt> --client-crt=<path client.crt> --client-key=<path client key>" )
53
- }
54
-
55
- client , err := newDexClient ("127.0.0.1:5557" , * caCrt , * clientCrt , * clientKey )
56
- if err != nil {
57
- log .Fatalf ("failed creating dex client: %v " , err )
58
- }
59
-
45
+ func createPassword (cli api.DexClient ) error {
60
46
p := api.Password {
61
47
Email : "test@example.com" ,
62
48
// bcrypt hash of the value "test1" with cost 10
@@ -70,19 +56,18 @@ func main() {
70
56
}
71
57
72
58
// Create password.
73
- if resp , err := client .CreatePassword (context .TODO (), createReq ); err != nil || resp .AlreadyExists {
59
+ if resp , err := cli .CreatePassword (context .TODO (), createReq ); err != nil || resp .AlreadyExists {
74
60
if resp .AlreadyExists {
75
- log . Fatalf ("Password %s already exists" , createReq .Password .Email )
61
+ return fmt . Errorf ("Password %s already exists" , createReq .Password .Email )
76
62
}
77
- log .Fatalf ("failed to create password: %v" , err )
78
- } else {
79
- log .Printf ("Created password with email %s" , createReq .Password .Email )
63
+ return fmt .Errorf ("failed to create password: %v" , err )
80
64
}
65
+ log .Printf ("Created password with email %s" , createReq .Password .Email )
81
66
82
67
// List all passwords.
83
- resp , err := client .ListPasswords (context .TODO (), & api.ListPasswordReq {})
68
+ resp , err := cli .ListPasswords (context .TODO (), & api.ListPasswordReq {})
84
69
if err != nil {
85
- log . Fatalf ("failed to list password: %v" , err )
70
+ return fmt . Errorf ("failed to list password: %v" , err )
86
71
}
87
72
88
73
log .Print ("Listing Passwords:\n " )
@@ -95,12 +80,33 @@ func main() {
95
80
}
96
81
97
82
// Delete password with email = test@example.com.
98
- if resp , err := client .DeletePassword (context .TODO (), deleteReq ); err != nil || resp .NotFound {
83
+ if resp , err := cli .DeletePassword (context .TODO (), deleteReq ); err != nil || resp .NotFound {
99
84
if resp .NotFound {
100
- log . Fatalf ("Password %s not found" , deleteReq .Email )
85
+ return fmt . Errorf ("Password %s not found" , deleteReq .Email )
101
86
}
102
- log .Fatalf ("failed to delete password: %v" , err )
103
- } else {
104
- log .Printf ("Deleted password with email %s" , deleteReq .Email )
87
+ return fmt .Errorf ("failed to delete password: %v" , err )
88
+ }
89
+ log .Printf ("Deleted password with email %s" , deleteReq .Email )
90
+
91
+ return nil
92
+ }
93
+
94
+ func main () {
95
+ caCrt := flag .String ("ca-crt" , "" , "CA certificate" )
96
+ clientCrt := flag .String ("client-crt" , "" , "Client certificate" )
97
+ clientKey := flag .String ("client-key" , "" , "Client key" )
98
+ flag .Parse ()
99
+
100
+ if * clientCrt == "" || * caCrt == "" || * clientKey == "" {
101
+ log .Fatal ("Please provide CA & client certificates and client key. Usage: ./client --ca-crt=<path ca.crt> --client-crt=<path client.crt> --client-key=<path client key>" )
102
+ }
103
+
104
+ client , err := newDexClient ("127.0.0.1:5557" , * caCrt , * clientCrt , * clientKey )
105
+ if err != nil {
106
+ log .Fatalf ("failed creating dex client: %v " , err )
107
+ }
108
+
109
+ if err := createPassword (client ); err != nil {
110
+ log .Fatalf ("testPassword failed: %v" , err )
105
111
}
106
112
}
0 commit comments