Skip to content

Commit cf8a501

Browse files
committed
Add a basic e2e test for X-Request-Id reflection
1 parent a58f595 commit cf8a501

File tree

5 files changed

+155
-40
lines changed

5 files changed

+155
-40
lines changed

api/api_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -884,16 +884,12 @@ func Test_Sign(t *testing.T) {
884884
CsrPEM: CertificateRequest{csr},
885885
OTT: "foobarzar",
886886
})
887-
if err != nil {
888-
t.Fatal(err)
889-
}
887+
require.NoError(t, err)
890888
invalid, err := json.Marshal(SignRequest{
891889
CsrPEM: CertificateRequest{csr},
892890
OTT: "",
893891
})
894-
if err != nil {
895-
t.Fatal(err)
896-
}
892+
require.NoError(t, err)
897893

898894
expected1 := []byte(`{"crt":"` + strings.ReplaceAll(certPEM, "\n", `\n`) + `\n","ca":"` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n","certChain":["` + strings.ReplaceAll(certPEM, "\n", `\n`) + `\n","` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n"]}`)
899895
expected2 := []byte(`{"crt":"` + strings.ReplaceAll(stepCertPEM, "\n", `\n`) + `\n","ca":"` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n","certChain":["` + strings.ReplaceAll(stepCertPEM, "\n", `\n`) + `\n","` + strings.ReplaceAll(rootPEM, "\n", `\n`) + `\n"]}`)

ca/ca_test.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ ZEp7knvU2psWRw==
289289

290290
if assert.Equals(t, rr.Code, tc.status) {
291291
body := &ClosingBuffer{rr.Body}
292+
resp := &http.Response{
293+
Body: body,
294+
}
292295
if rr.Code < http.StatusBadRequest {
293296
var sign api.SignResponse
294297
assert.FatalError(t, readJSON(body, &sign))
@@ -325,7 +328,7 @@ ZEp7knvU2psWRw==
325328
assert.FatalError(t, err)
326329
assert.Equals(t, intermediate, realIntermediate)
327330
} else {
328-
err := readError(body)
331+
err := readError(resp)
329332
if tc.errMsg == "" {
330333
assert.FatalError(t, errors.New("must validate response error"))
331334
}
@@ -369,6 +372,9 @@ func TestCAProvisioners(t *testing.T) {
369372

370373
if assert.Equals(t, rr.Code, tc.status) {
371374
body := &ClosingBuffer{rr.Body}
375+
resp := &http.Response{
376+
Body: body,
377+
}
372378
if rr.Code < http.StatusBadRequest {
373379
var resp api.ProvisionersResponse
374380

@@ -379,7 +385,7 @@ func TestCAProvisioners(t *testing.T) {
379385
assert.FatalError(t, err)
380386
assert.Equals(t, a, b)
381387
} else {
382-
err := readError(body)
388+
err := readError(resp)
383389
if tc.errMsg == "" {
384390
assert.FatalError(t, errors.New("must validate response error"))
385391
}
@@ -436,12 +442,15 @@ func TestCAProvisionerEncryptedKey(t *testing.T) {
436442

437443
if assert.Equals(t, rr.Code, tc.status) {
438444
body := &ClosingBuffer{rr.Body}
445+
resp := &http.Response{
446+
Body: body,
447+
}
439448
if rr.Code < http.StatusBadRequest {
440449
var ek api.ProvisionerKeyResponse
441450
assert.FatalError(t, readJSON(body, &ek))
442451
assert.Equals(t, ek.Key, tc.expectedKey)
443452
} else {
444-
err := readError(body)
453+
err := readError(resp)
445454
if tc.errMsg == "" {
446455
assert.FatalError(t, errors.New("must validate response error"))
447456
}
@@ -498,12 +507,15 @@ func TestCARoot(t *testing.T) {
498507

499508
if assert.Equals(t, rr.Code, tc.status) {
500509
body := &ClosingBuffer{rr.Body}
510+
resp := &http.Response{
511+
Body: body,
512+
}
501513
if rr.Code < http.StatusBadRequest {
502514
var root api.RootResponse
503515
assert.FatalError(t, readJSON(body, &root))
504516
assert.Equals(t, root.RootPEM.Certificate, rootCrt)
505517
} else {
506-
err := readError(body)
518+
err := readError(resp)
507519
if tc.errMsg == "" {
508520
assert.FatalError(t, errors.New("must validate response error"))
509521
}
@@ -641,6 +653,9 @@ func TestCARenew(t *testing.T) {
641653

642654
if assert.Equals(t, rr.Code, tc.status) {
643655
body := &ClosingBuffer{rr.Body}
656+
resp := &http.Response{
657+
Body: body,
658+
}
644659
if rr.Code < http.StatusBadRequest {
645660
var sign api.SignResponse
646661
assert.FatalError(t, readJSON(body, &sign))
@@ -673,7 +688,7 @@ func TestCARenew(t *testing.T) {
673688

674689
assert.Equals(t, *sign.TLSOptions, authority.DefaultTLSOptions)
675690
} else {
676-
err := readError(body)
691+
err := readError(resp)
677692
if tc.errMsg == "" {
678693
assert.FatalError(t, errors.New("must validate response error"))
679694
}

ca/client.go

+26-25
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ retry:
622622
retried = true
623623
goto retry
624624
}
625-
return nil, readError(resp.Body)
625+
return nil, readError(resp)
626626
}
627627
var version api.VersionResponse
628628
if err := readJSON(resp.Body, &version); err != nil {
@@ -652,7 +652,7 @@ retry:
652652
retried = true
653653
goto retry
654654
}
655-
return nil, readError(resp.Body)
655+
return nil, readError(resp)
656656
}
657657
var health api.HealthResponse
658658
if err := readJSON(resp.Body, &health); err != nil {
@@ -687,7 +687,7 @@ retry:
687687
retried = true
688688
goto retry
689689
}
690-
return nil, readError(resp.Body)
690+
return nil, readError(resp)
691691
}
692692
var root api.RootResponse
693693
if err := readJSON(resp.Body, &root); err != nil {
@@ -726,7 +726,7 @@ retry:
726726
retried = true
727727
goto retry
728728
}
729-
return nil, readError(resp.Body)
729+
return nil, readError(resp)
730730
}
731731
var sign api.SignResponse
732732
if err := readJSON(resp.Body, &sign); err != nil {
@@ -765,7 +765,7 @@ retry:
765765
retried = true
766766
goto retry
767767
}
768-
return nil, readError(resp.Body)
768+
return nil, readError(resp)
769769
}
770770
var sign api.SignResponse
771771
if err := readJSON(resp.Body, &sign); err != nil {
@@ -802,7 +802,7 @@ retry:
802802
retried = true
803803
goto retry
804804
}
805-
return nil, readError(resp.Body)
805+
return nil, readError(resp)
806806
}
807807
var sign api.SignResponse
808808
if err := readJSON(resp.Body, &sign); err != nil {
@@ -842,7 +842,7 @@ retry:
842842
retried = true
843843
goto retry
844844
}
845-
return nil, readError(resp.Body)
845+
return nil, readError(resp)
846846
}
847847
var sign api.SignResponse
848848
if err := readJSON(resp.Body, &sign); err != nil {
@@ -883,7 +883,7 @@ retry:
883883
retried = true
884884
goto retry
885885
}
886-
return nil, readError(resp.Body)
886+
return nil, readError(resp)
887887
}
888888
var revoke api.RevokeResponse
889889
if err := readJSON(resp.Body, &revoke); err != nil {
@@ -926,7 +926,7 @@ retry:
926926
retried = true
927927
goto retry
928928
}
929-
return nil, readError(resp.Body)
929+
return nil, readError(resp)
930930
}
931931
var provisioners api.ProvisionersResponse
932932
if err := readJSON(resp.Body, &provisioners); err != nil {
@@ -958,7 +958,7 @@ retry:
958958
retried = true
959959
goto retry
960960
}
961-
return nil, readError(resp.Body)
961+
return nil, readError(resp)
962962
}
963963
var key api.ProvisionerKeyResponse
964964
if err := readJSON(resp.Body, &key); err != nil {
@@ -988,7 +988,7 @@ retry:
988988
retried = true
989989
goto retry
990990
}
991-
return nil, readError(resp.Body)
991+
return nil, readError(resp)
992992
}
993993
var roots api.RootsResponse
994994
if err := readJSON(resp.Body, &roots); err != nil {
@@ -1018,7 +1018,7 @@ retry:
10181018
retried = true
10191019
goto retry
10201020
}
1021-
return nil, readError(resp.Body)
1021+
return nil, readError(resp)
10221022
}
10231023
var federation api.FederationResponse
10241024
if err := readJSON(resp.Body, &federation); err != nil {
@@ -1052,7 +1052,7 @@ retry:
10521052
retried = true
10531053
goto retry
10541054
}
1055-
return nil, readError(resp.Body)
1055+
return nil, readError(resp)
10561056
}
10571057
var sign api.SSHSignResponse
10581058
if err := readJSON(resp.Body, &sign); err != nil {
@@ -1086,7 +1086,7 @@ retry:
10861086
retried = true
10871087
goto retry
10881088
}
1089-
return nil, readError(resp.Body)
1089+
return nil, readError(resp)
10901090
}
10911091
var renew api.SSHRenewResponse
10921092
if err := readJSON(resp.Body, &renew); err != nil {
@@ -1120,7 +1120,7 @@ retry:
11201120
retried = true
11211121
goto retry
11221122
}
1123-
return nil, readError(resp.Body)
1123+
return nil, readError(resp)
11241124
}
11251125
var rekey api.SSHRekeyResponse
11261126
if err := readJSON(resp.Body, &rekey); err != nil {
@@ -1154,7 +1154,7 @@ retry:
11541154
retried = true
11551155
goto retry
11561156
}
1157-
return nil, readError(resp.Body)
1157+
return nil, readError(resp)
11581158
}
11591159
var revoke api.SSHRevokeResponse
11601160
if err := readJSON(resp.Body, &revoke); err != nil {
@@ -1184,7 +1184,7 @@ retry:
11841184
retried = true
11851185
goto retry
11861186
}
1187-
return nil, readError(resp.Body)
1187+
return nil, readError(resp)
11881188
}
11891189
var keys api.SSHRootsResponse
11901190
if err := readJSON(resp.Body, &keys); err != nil {
@@ -1214,7 +1214,7 @@ retry:
12141214
retried = true
12151215
goto retry
12161216
}
1217-
return nil, readError(resp.Body)
1217+
return nil, readError(resp)
12181218
}
12191219
var keys api.SSHRootsResponse
12201220
if err := readJSON(resp.Body, &keys); err != nil {
@@ -1248,7 +1248,7 @@ retry:
12481248
retried = true
12491249
goto retry
12501250
}
1251-
return nil, readError(resp.Body)
1251+
return nil, readError(resp)
12521252
}
12531253
var cfg api.SSHConfigResponse
12541254
if err := readJSON(resp.Body, &cfg); err != nil {
@@ -1287,7 +1287,7 @@ retry:
12871287
retried = true
12881288
goto retry
12891289
}
1290-
return nil, readError(resp.Body)
1290+
return nil, readError(resp)
12911291
}
12921292
var check api.SSHCheckPrincipalResponse
12931293
if err := readJSON(resp.Body, &check); err != nil {
@@ -1316,7 +1316,7 @@ retry:
13161316
retried = true
13171317
goto retry
13181318
}
1319-
return nil, readError(resp.Body)
1319+
return nil, readError(resp)
13201320
}
13211321
var hosts api.SSHGetHostsResponse
13221322
if err := readJSON(resp.Body, &hosts); err != nil {
@@ -1348,7 +1348,7 @@ retry:
13481348
retried = true
13491349
goto retry
13501350
}
1351-
return nil, readError(resp.Body)
1351+
return nil, readError(resp)
13521352
}
13531353
var bastion api.SSHBastionResponse
13541354
if err := readJSON(resp.Body, &bastion); err != nil {
@@ -1516,12 +1516,13 @@ func readProtoJSON(r io.ReadCloser, m proto.Message) error {
15161516
return protojson.Unmarshal(data, m)
15171517
}
15181518

1519-
func readError(r io.ReadCloser) error {
1520-
defer r.Close()
1519+
func readError(r *http.Response) error {
1520+
defer r.Body.Close()
15211521
apiErr := new(errs.Error)
1522-
if err := json.NewDecoder(r).Decode(apiErr); err != nil {
1522+
if err := json.NewDecoder(r.Body).Decode(apiErr); err != nil {
15231523
return err
15241524
}
1525+
apiErr.RequestID = r.Header.Get("X-Request-Id")
15251526
return apiErr
15261527
}
15271528

errs/error.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ func WithKeyVal(key string, val interface{}) Option {
4949

5050
// Error represents the CA API errors.
5151
type Error struct {
52-
Status int
53-
Err error
54-
Msg string
55-
Details map[string]interface{}
52+
Status int
53+
Err error
54+
Msg string
55+
Details map[string]interface{}
56+
RequestID string `json:"-"`
5657
}
5758

5859
// ErrorResponse represents an error in JSON format.

0 commit comments

Comments
 (0)