@@ -15,42 +15,43 @@ func (s *Service) handleStatusEndpoint(w http.ResponseWriter, r *http.Request) {
1515 return
1616 }
1717
18- if len (s .options .Users ) == 0 {
19- writeJSONError (w , r , http .StatusForbidden , "authentication_error" , "status endpoint requires user authentication" )
20- return
21- }
22-
23- if r .Header .Get ("X-Api-Key" ) != "" || r .Header .Get ("Api-Key" ) != "" {
24- writeJSONError (w , r , http .StatusBadRequest , "invalid_request_error" ,
25- "API key authentication is not supported; use Authorization: Bearer with an OCM user token" )
26- return
27- }
18+ var provider credentialProvider
19+ var userConfig * option.OCMUser
20+ if len (s .options .Users ) > 0 {
21+ if r .Header .Get ("X-Api-Key" ) != "" || r .Header .Get ("Api-Key" ) != "" {
22+ writeJSONError (w , r , http .StatusBadRequest , "invalid_request_error" ,
23+ "API key authentication is not supported; use Authorization: Bearer with an OCM user token" )
24+ return
25+ }
2826
29- authHeader := r .Header .Get ("Authorization" )
30- if authHeader == "" {
31- writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "missing api key" )
32- return
33- }
34- clientToken := strings .TrimPrefix (authHeader , "Bearer " )
35- if clientToken == authHeader {
36- writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "invalid api key format" )
37- return
38- }
39- username , ok := s .userManager .Authenticate (clientToken )
40- if ! ok {
41- writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "invalid api key" )
42- return
43- }
27+ authHeader := r .Header .Get ("Authorization" )
28+ if authHeader == "" {
29+ writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "missing api key" )
30+ return
31+ }
32+ clientToken := strings .TrimPrefix (authHeader , "Bearer " )
33+ if clientToken == authHeader {
34+ writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "invalid api key format" )
35+ return
36+ }
37+ username , ok := s .userManager .Authenticate (clientToken )
38+ if ! ok {
39+ writeJSONError (w , r , http .StatusUnauthorized , "authentication_error" , "invalid api key" )
40+ return
41+ }
4442
45- userConfig := s .userConfigMap [username ]
46- if userConfig == nil {
47- writeJSONError (w , r , http .StatusInternalServerError , "api_error" , "user config not found" )
48- return
43+ userConfig = s .userConfigMap [username ]
44+ var err error
45+ provider , err = credentialForUser (s .userConfigMap , s .providers , username )
46+ if err != nil {
47+ writeJSONError (w , r , http .StatusInternalServerError , "api_error" , err .Error ())
48+ return
49+ }
50+ } else {
51+ provider = noUserCredentialProvider (s .providers , s .options )
4952 }
50-
51- provider , err := credentialForUser (s .userConfigMap , s .providers , s .legacyProvider , username )
52- if err != nil {
53- writeJSONError (w , r , http .StatusInternalServerError , "api_error" , err .Error ())
53+ if provider == nil {
54+ writeJSONError (w , r , http .StatusInternalServerError , "api_error" , "no credential available" )
5455 return
5556 }
5657
@@ -72,10 +73,10 @@ func (s *Service) computeAggregatedUtilization(provider credentialProvider, user
7273 if ! credential .isAvailable () {
7374 continue
7475 }
75- if userConfig .ExternalCredential != "" && credential .tagName () == userConfig .ExternalCredential {
76+ if userConfig != nil && userConfig .ExternalCredential != "" && credential .tagName () == userConfig .ExternalCredential {
7677 continue
7778 }
78- if ! userConfig .AllowExternalUsage && credential .isExternal () {
79+ if userConfig != nil && ! userConfig .AllowExternalUsage && credential .isExternal () {
7980 continue
8081 }
8182 weight := credential .planWeight ()
@@ -100,7 +101,7 @@ func (s *Service) computeAggregatedUtilization(provider credentialProvider, user
100101}
101102
102103func (s * Service ) rewriteResponseHeadersForExternalUser (headers http.Header , userConfig * option.OCMUser ) {
103- provider , err := credentialForUser (s .userConfigMap , s .providers , s . legacyProvider , userConfig .Name )
104+ provider , err := credentialForUser (s .userConfigMap , s .providers , userConfig .Name )
104105 if err != nil {
105106 return
106107 }
0 commit comments