Skip to content

Commit

Permalink
chore: removed old api group
Browse files Browse the repository at this point in the history
The old rancher API group was marked for removal for the next release

Signed-off-by: Bruno Bressi <[email protected]>
  • Loading branch information
puffitos committed Jan 21, 2025
1 parent 726a7c5 commit e5bc3e1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 35 deletions.
6 changes: 3 additions & 3 deletions pkg/agent/http_api_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *apiContext) responseJSON(data interface{}) (err error) {
}
})

return
return err
}

func (c *apiContext) responseProto(data proto.Message) (err error) {
Expand All @@ -92,7 +92,7 @@ func (c *apiContext) responseProto(data proto.Message) (err error) {
}
})

return
return err
}

func (c *apiContext) responseMetrics(data *promgo.MetricFamily) (err error) {
Expand All @@ -112,7 +112,7 @@ func (c *apiContext) responseMetrics(data *promgo.MetricFamily) (err error) {
}
})

return
return err
}

func (c *apiContext) proxyWith(request *http.Request) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/http_hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func hijackRead(apiCtx *apiContext) error {
size := len(rawQueries)

results := make([]*prompb.QueryResult, 0, size)
for i := 0; i < size; i++ {
for range size {
results = append(results, &prompb.QueryResult{})
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"
"unsafe"

"github.com/juju/errors"
"github.com/prometheus/prometheus/promql/promqltest"

"github.com/caas-team/prometheus-auth/pkg/agent/test"
Expand Down Expand Up @@ -551,7 +552,7 @@ func (v ScenarioValidator) validateProtoBody(t *testing.T, res *httptest.Respons
}

func (v ScenarioValidator) validateJSONBody(t *testing.T, res *httptest.ResponseRecorder) {
if got, want := string(res.Body.Bytes()), jsonResponseBody(v.Scenario.RespBody); got != want {
if got, want := res.Body.String(), jsonResponseBody(v.Scenario.RespBody); got != want {
t.Errorf("[%s] [%s] token %q scenario %q: got body\n%s\n, want\n%s\n", v.Type, v.Method, v.Token, v.Name, got, want)
}
}
Expand Down Expand Up @@ -641,7 +642,7 @@ type fakeTokenAuth struct {
func (f *fakeTokenAuth) Authenticate(token string) (authentication.UserInfo, error) {
userInfo, ok := f.token2UserInfo[token]
if !ok {
return userInfo, fmt.Errorf("user is not authenticated")
return userInfo, errors.New("user is not authenticated")
}
return userInfo, nil
}
Expand Down
31 changes: 5 additions & 26 deletions pkg/kube/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,37 +131,16 @@ func (n *namespaces) validate(token string) (string, error) {
return "", errors.Annotatef(err, "failed to review token")
}

if !reviewResult.Status.Allowed || reviewResult.Status.Denied {
return "", fmt.Errorf("token is not allowed to access namespace %q", claimNamespace)
}

if reviewResult.Status.Allowed {
n.reviewResultTTLCache.Add(token, struct{}{}, 5*time.Minute)
return claimNamespace, nil
}

// DEPRECATED: this is to ensure backward compatibility with old monitoring.cattle.io group
// it'll be removed in the next release.
sar = &authorization.SubjectAccessReview{
Spec: authorization.SubjectAccessReviewSpec{
ResourceAttributes: &authorization.ResourceAttributes{
Namespace: claimNamespace,
Verb: "view",
Group: "monitoring.cattle.io",
Resource: "prometheus",
},
User: sarUser,
},
}

reviewResult, err = n.subjectAccessReviewsClient.Create(context.TODO(), sar, meta.CreateOptions{})
if err != nil {
return "", errors.Annotatef(err, "failed to review token")
}
// if this also doesn't validate, return the error
// move after error check after removing the second subject access review
if !reviewResult.Status.Allowed || reviewResult.Status.Denied {
return "", fmt.Errorf("token is not allowed to access namespace %q", claimNamespace)
}

log.Warnf("namespace %q is still using the deprecated monitoring.cattle.io group", claimNamespace)
n.reviewResultTTLCache.Add(token, struct{}{}, 5*time.Minute)
log.Debugf("token is not allowed to access namespace %q, denied: %s", claimNamespace, reviewResult.Status.Reason)

return claimNamespace, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/prom/matcher.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package prom

import (
"fmt"
"errors"

promlb "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
Expand Down Expand Up @@ -86,7 +86,7 @@ func toLabelMatchers(matchers []*promlb.Matcher) ([]*prompb.LabelMatcher, error)
case promlb.MatchNotRegexp:
mType = prompb.LabelMatcher_NRE
default:
return nil, fmt.Errorf("invalid matcher type")
return nil, errors.New("invalid matcher type")
}
pbMatchers = append(pbMatchers, &prompb.LabelMatcher{
Type: mType,
Expand All @@ -111,7 +111,7 @@ func fromLabelMatchers(matchers []*prompb.LabelMatcher) ([]*promlb.Matcher, erro
case prompb.LabelMatcher_NRE:
mtype = promlb.MatchNotRegexp
default:
return nil, fmt.Errorf("invalid matcher type")
return nil, errors.New("invalid matcher type")
}
matcher, err := promlb.NewMatcher(mtype, matcher.Name, matcher.Value)
if err != nil {
Expand Down

0 comments on commit e5bc3e1

Please sign in to comment.