Skip to content

Commit 880b0be

Browse files
committed
address review comments
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
1 parent 12a924d commit 880b0be

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

pkg/datagatherer/oidc/oidc.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/jetstack/preflight/pkg/kubeconfig"
1818
)
1919

20-
// OIDCDiscovery contains the configuration for the k8s-discovery data-gatherer
20+
// OIDCDiscovery contains the configuration for the oidc data-gatherer.
2121
type OIDCDiscovery struct {
2222
// KubeConfigPath is the path to the kubeconfig file. If empty, will assume it runs in-cluster.
2323
KubeConfigPath string `yaml:"kubeconfig"`
@@ -49,7 +49,7 @@ func (c *OIDCDiscovery) NewDataGatherer(ctx context.Context) (datagatherer.DataG
4949
}, nil
5050
}
5151

52-
// DataGathererOIDC stores the config for a k8s-discovery datagatherer
52+
// DataGathererOIDC stores the config for an oidc datagatherer.
5353
type DataGathererOIDC struct {
5454
cl rest.Interface
5555
}
@@ -79,6 +79,13 @@ func (g *DataGathererOIDC) Fetch() (any, int, error) {
7979
return ""
8080
}
8181

82+
if oidcErr != nil {
83+
klog.FromContext(ctx).V(4).Error(oidcErr, "Failed to fetch OIDC configuration")
84+
}
85+
if jwksErr != nil {
86+
klog.FromContext(ctx).V(4).Error(jwksErr, "Failed to fetch JWKS")
87+
}
88+
8289
return &api.OIDCDiscoveryData{
8390
OIDCConfig: oidcResponse,
8491
OIDCConfigError: errToString(oidcErr),
@@ -97,7 +104,7 @@ func (g *DataGathererOIDC) fetchOIDCConfig(ctx context.Context) (map[string]any,
97104
bytes, _ := result.Raw() // we already checked result.Error(), so there is no error here
98105
var oidcResponse map[string]any
99106
if err := json.Unmarshal(bytes, &oidcResponse); err != nil {
100-
return nil, fmt.Errorf("failed to unmarshal OIDC discovery document: %v", err)
107+
return nil, fmt.Errorf("failed to unmarshal OIDC discovery document: %v (raw: %q)", err, stringFirstN(string(bytes), 80))
101108
}
102109

103110
return oidcResponse, nil
@@ -118,12 +125,19 @@ func (g *DataGathererOIDC) fetchJWKS(ctx context.Context) (map[string]any, error
118125
bytes, _ := result.Raw() // we already checked result.Error(), so there is no error here
119126
var jwksResponse map[string]any
120127
if err := json.Unmarshal(bytes, &jwksResponse); err != nil {
121-
return nil, fmt.Errorf("failed to unmarshal JWKS response: %v", err)
128+
return nil, fmt.Errorf("failed to unmarshal JWKS response: %v (raw: %q)", err, stringFirstN(string(bytes), 80))
122129
}
123130

124131
return jwksResponse, nil
125132
}
126133

134+
func stringFirstN(s string, n int) string {
135+
if len(s) <= n {
136+
return s
137+
}
138+
return s[:n]
139+
}
140+
127141
// based on https://github.com/kubernetes/kubectl/blob/a64ceaeab69eed1f11a9e1bd91cf2c1446de811c/pkg/cmd/util/helpers.go#L244
128142
func k8sErrorMessage(err error) string {
129143
if status, isStatus := err.(apierrors.APIStatus); isStatus {
@@ -142,7 +156,6 @@ func k8sErrorMessage(err error) string {
142156
}
143157

144158
if t, isURL := err.(*url.Error); isURL {
145-
klog.V(4).Infof("Connection error: %s %s: %v", t.Op, t.URL, t.Err)
146159
if strings.Contains(t.Err.Error(), "connection refused") {
147160
host := t.URL
148161
if server, err := url.Parse(t.URL); err == nil {

pkg/datagatherer/oidc/oidc_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package oidc
22

33
import (
4+
"bytes"
45
"net/http"
56
"net/http/httptest"
67
"net/url"
78
"testing"
89

10+
"github.com/stretchr/testify/require"
911
"k8s.io/client-go/discovery"
1012
"k8s.io/client-go/rest"
1113

1214
"github.com/jetstack/preflight/api"
13-
"github.com/stretchr/testify/require"
1415
)
1516

1617
func makeRESTClient(t *testing.T, ts *httptest.Server) rest.Interface {
@@ -94,13 +95,14 @@ func TestFetch_Errors(t *testing.T) {
9495
},
9596
jwksResponse: func(w http.ResponseWriter, r *http.Request) {
9697
w.Header().Set("Content-Type", "application/json")
97-
_, _ = w.Write([]byte(`}0`))
98+
_, _ = w.Write([]byte(`}`))
99+
_, _ = w.Write(bytes.Repeat([]byte{'0'}, 5000))
98100
},
99-
expOIDCConfigError: "failed to unmarshal OIDC discovery document: invalid character '}' looking for beginning of value",
100-
expJWKSError: "failed to unmarshal JWKS response: invalid character '}' looking for beginning of value",
101+
expOIDCConfigError: `failed to unmarshal OIDC discovery document: invalid character '}' looking for beginning of value (raw: "}{")`,
102+
expJWKSError: `failed to unmarshal JWKS response: invalid character '}' looking for beginning of value (raw: "}0000000000000000000000000000000000000000000000000000000000000000000000000000000")`,
101103
},
102104
{
103-
name: "permission error (no body)",
105+
name: "Forbidden error (no body)",
104106
openidConfigurationResponse: func(w http.ResponseWriter, r *http.Request) {
105107
http.Error(w, "forbidden", http.StatusForbidden)
106108
},
@@ -111,7 +113,7 @@ func TestFetch_Errors(t *testing.T) {
111113
expJWKSError: "failed to get /openid/v1/jwks: Error from server (Forbidden): forbidden",
112114
},
113115
{
114-
name: "permission error (*metav1.Status body)",
116+
name: "Forbidden error (*metav1.Status body)",
115117
openidConfigurationResponse: func(w http.ResponseWriter, r *http.Request) {
116118
w.Header().Set("Content-Type", "application/json")
117119
w.WriteHeader(http.StatusForbidden)
@@ -143,6 +145,37 @@ func TestFetch_Errors(t *testing.T) {
143145
expOIDCConfigError: `failed to get /.well-known/openid-configuration: Error from server (Forbidden): forbidden: User "system:serviceaccount:default:test" cannot get path "/.well-known/openid-configuration"`,
144146
expJWKSError: `failed to get /openid/v1/jwks: Error from server (Forbidden): forbidden: User "system:serviceaccount:default:test" cannot get path "/openid/v1/jwks"`,
145147
},
148+
{
149+
name: "Unauthorized error (*metav1.Status body)",
150+
openidConfigurationResponse: func(w http.ResponseWriter, r *http.Request) {
151+
w.Header().Set("Content-Type", "application/json")
152+
w.WriteHeader(http.StatusForbidden)
153+
_, _ = w.Write([]byte(`{
154+
"kind": "Status",
155+
"apiVersion": "v1",
156+
"metadata": {},
157+
"status": "Failure",
158+
"message": "Unauthorized",
159+
"reason": "Unauthorized",
160+
"code": 401
161+
}`))
162+
},
163+
jwksResponse: func(w http.ResponseWriter, r *http.Request) {
164+
w.Header().Set("Content-Type", "application/json")
165+
w.WriteHeader(http.StatusForbidden)
166+
_, _ = w.Write([]byte(`{
167+
"kind": "Status",
168+
"apiVersion": "v1",
169+
"metadata": {},
170+
"status": "Failure",
171+
"message": "Unauthorized",
172+
"reason": "Unauthorized",
173+
"code": 401
174+
}`))
175+
},
176+
expOIDCConfigError: `failed to get /.well-known/openid-configuration: error: You must be logged in to the server (Unauthorized)`,
177+
expJWKSError: `failed to get /openid/v1/jwks: error: You must be logged in to the server (Unauthorized)`,
178+
},
146179
}
147180

148181
for _, tc := range tests {

0 commit comments

Comments
 (0)