@@ -2,12 +2,14 @@ package ca
2
2
3
3
import (
4
4
"context"
5
+ "crypto"
5
6
"crypto/tls"
6
7
"net"
7
8
"net/http"
8
9
"strings"
9
10
10
11
"github.com/pkg/errors"
12
+ "github.com/smallstep/certificates/api"
11
13
"go.step.sm/crypto/jose"
12
14
)
13
15
@@ -58,22 +60,7 @@ func Bootstrap(token string) (*Client, error) {
58
60
// }
59
61
// resp, err := client.Get("https://internal.smallstep.com")
60
62
func BootstrapClient (ctx context.Context , token string , options ... TLSOption ) (* http.Client , error ) {
61
- client , err := Bootstrap (token )
62
- if err != nil {
63
- return nil , err
64
- }
65
-
66
- version , err := client .Version ()
67
- if err != nil {
68
- return nil , err
69
- }
70
-
71
- req , pk , err := CreateSignRequest (token )
72
- if err != nil {
73
- return nil , err
74
- }
75
-
76
- sign , err := client .Sign (req )
63
+ b , err := createBootstrap (token )
77
64
if err != nil {
78
65
return nil , err
79
66
}
@@ -83,11 +70,11 @@ func BootstrapClient(ctx context.Context, token string, options ...TLSOption) (*
83
70
// The roots request is only supported if identity certificates are not
84
71
// required. In all cases the current root is also added after applying all
85
72
// options too.
86
- if ! version . RequireClientAuthentication {
73
+ if ! b . RequireClientAuth {
87
74
options = append (options , AddRootsToRootCAs ())
88
75
}
89
76
90
- transport , err := client . Transport (ctx , sign , pk , options ... )
77
+ transport , err := b . Client . Transport (ctx , b . SignResponse , b . PrivateKey , options ... )
91
78
if err != nil {
92
79
return nil , err
93
80
}
@@ -131,22 +118,7 @@ func BootstrapServer(ctx context.Context, token string, base *http.Server, optio
131
118
return nil , errors .New ("server TLSConfig is already set" )
132
119
}
133
120
134
- client , err := Bootstrap (token )
135
- if err != nil {
136
- return nil , err
137
- }
138
-
139
- version , err := client .Version ()
140
- if err != nil {
141
- return nil , err
142
- }
143
-
144
- req , pk , err := CreateSignRequest (token )
145
- if err != nil {
146
- return nil , err
147
- }
148
-
149
- sign , err := client .Sign (req )
121
+ b , err := createBootstrap (token )
150
122
if err != nil {
151
123
return nil , err
152
124
}
@@ -156,11 +128,11 @@ func BootstrapServer(ctx context.Context, token string, base *http.Server, optio
156
128
// The roots request is only supported if identity certificates are not
157
129
// required. In all cases the current root is also added after applying all
158
130
// options too.
159
- if ! version . RequireClientAuthentication {
131
+ if ! b . RequireClientAuth {
160
132
options = append (options , AddRootsToCAs ())
161
133
}
162
134
163
- tlsConfig , err := client . GetServerTLSConfig (ctx , sign , pk , options ... )
135
+ tlsConfig , err := b . Client . GetServerTLSConfig (ctx , b . SignResponse , b . PrivateKey , options ... )
164
136
if err != nil {
165
137
return nil , err
166
138
}
@@ -194,39 +166,60 @@ func BootstrapServer(ctx context.Context, token string, base *http.Server, optio
194
166
// ... // register services
195
167
// srv.Serve(lis)
196
168
func BootstrapListener (ctx context.Context , token string , inner net.Listener , options ... TLSOption ) (net.Listener , error ) {
197
- client , err := Bootstrap (token )
169
+ b , err := createBootstrap (token )
198
170
if err != nil {
199
171
return nil , err
200
172
}
201
173
202
- version , err := client .Version ()
174
+ // Make sure the tlsConfig have all supported roots on RootCAs.
175
+ //
176
+ // The roots request is only supported if identity certificates are not
177
+ // required. In all cases the current root is also added after applying all
178
+ // options too.
179
+ if ! b .RequireClientAuth {
180
+ options = append (options , AddRootsToCAs ())
181
+ }
182
+
183
+ tlsConfig , err := b .Client .GetServerTLSConfig (ctx , b .SignResponse , b .PrivateKey , options ... )
203
184
if err != nil {
204
185
return nil , err
205
186
}
206
187
207
- req , pk , err := CreateSignRequest (token )
188
+ return tls .NewListener (inner , tlsConfig ), nil
189
+ }
190
+
191
+ type bootstrap struct {
192
+ Client * Client
193
+ RequireClientAuth bool
194
+ SignResponse * api.SignResponse
195
+ PrivateKey crypto.PrivateKey
196
+ }
197
+
198
+ func createBootstrap (token string ) (* bootstrap , error ) {
199
+ client , err := Bootstrap (token )
208
200
if err != nil {
209
201
return nil , err
210
202
}
211
203
212
- sign , err := client .Sign ( req )
204
+ version , err := client .Version ( )
213
205
if err != nil {
214
206
return nil , err
215
207
}
216
208
217
- // Make sure the tlsConfig have all supported roots on RootCAs.
218
- //
219
- // The roots request is only supported if identity certificates are not
220
- // required. In all cases the current root is also added after applying all
221
- // options too.
222
- if ! version .RequireClientAuthentication {
223
- options = append (options , AddRootsToCAs ())
209
+ req , pk , err := CreateSignRequest (token )
210
+ if err != nil {
211
+ return nil , err
224
212
}
225
213
226
- tlsConfig , err := client .GetServerTLSConfig ( ctx , sign , pk , options ... )
214
+ sign , err := client .Sign ( req )
227
215
if err != nil {
228
216
return nil , err
229
217
}
230
218
231
- return tls .NewListener (inner , tlsConfig ), nil
219
+ return & bootstrap {
220
+ Client : client ,
221
+ RequireClientAuth : version .RequireClientAuthentication ,
222
+ SignResponse : sign ,
223
+ PrivateKey : pk ,
224
+ }, nil
232
225
}
0 commit comments