Skip to content

Commit

Permalink
ROX-27432: allow regex in authentication claimrules (#1441)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommartensen authored Dec 18, 2024
1 parent 5a2d4be commit 098706a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/PR.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
run: |
ENVIRONMENT=development TEST_MODE=true make install-argo clean-argo-config install-monitoring helm-deploy
sleep 10 # wait for old pods to disappear so the svc port-forward doesn't connect to them
kubectl -n infra port-forward svc/infra-server-service 8443:8443 &
kubectl -n infra port-forward svc/infra-server-service 8443:8443 > /dev/null 2>&1 &
sleep 10
kubectl -n infra logs -l app=infra-server --tail=-1
Expand All @@ -115,7 +115,7 @@ jobs:
- name: Check the deployment
run: |
kubectl -n infra port-forward svc/infra-server-service 8443:8443 &
kubectl -n infra port-forward svc/infra-server-service 8443:8443 > /dev/null 2>&1 &
sleep 10
version="$($INFRACTL version --json)"
Expand Down Expand Up @@ -157,7 +157,7 @@ jobs:
env:
INFRA_TOKEN: ${{ secrets.INFRA_TOKEN_DEV }}
run: |
kubectl -n infra port-forward svc/infra-server-service 8443:8443 &
kubectl -n infra port-forward svc/infra-server-service 8443:8443 > /dev/null 2>&1 &
sleep 5
$INFRACTL whoami || true
Expand All @@ -173,6 +173,6 @@ jobs:
env:
INFRA_TOKEN: ${{ secrets.INFRA_TOKEN_DEV }}
run: |
kubectl -n infra port-forward svc/infra-server-service 8443:8443 &
kubectl -n infra port-forward svc/infra-server-service 8443:8443 > /dev/null 2>&1 &
sleep 5
make go-e2e-tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ nohup.out
test/mocks
__debug_bin*
.DS_Store
report.xml
8 changes: 7 additions & 1 deletion auth/claimrule/claim_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/jeremywohl/flatten/v2"
Expand Down Expand Up @@ -70,9 +71,14 @@ func (cr *ClaimRule) equalCheck(flatTokenClaims map[string]interface{}, jsonPath
return errors.Errorf("expected claim %q is not found", jsonPath)
}

if cr.Value != tokenClaimValue {
pattern := fmt.Sprintf("^%s$", cr.Value)
found, err := regexp.MatchString(pattern, tokenClaimValue.(string))
if !found {
return errors.Errorf("expected claim %q is not correct", jsonPath)
}
if err != nil {
return errors.Wrapf(err, "error matching claim %s to expected value", tokenClaimValue)
}

return nil
}
Expand Down
55 changes: 55 additions & 0 deletions auth/claimrule/claim_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,61 @@ func getDataSets() map[string]dataSet {
}},
err: true,
},
"eq-regex-match": {
tokenClaims: map[string]interface{}{
"field": "val1",
},
rules: ClaimRules{{
Value: "(val1|val2)",
Path: "field",
Op: "eq",
}},
err: false,
},
"eq-regex-no-match": {
tokenClaims: map[string]interface{}{
"field": "val3",
},
rules: ClaimRules{{
Value: "(val1|val2)",
Path: "field",
Op: "eq",
}},
err: true,
},
"eq-regex-no-match-substring-claim": {
tokenClaims: map[string]interface{}{
"field": "val",
},
rules: ClaimRules{{
Value: "val1",
Path: "field",
Op: "eq",
}},
err: true,
},
"eq-regex-no-match-substring-rule": {
tokenClaims: map[string]interface{}{
"field": "val23",
},
rules: ClaimRules{{
Value: "val2",
Path: "field",
Op: "eq",
}},
err: true,
},
"in-regex-match": {
tokenClaims: map[string]interface{}{
"field": []string{"val1", "val2"},
},
rules: ClaimRules{{
Value: "(val2|val3)",
Path: "field",
Op: "in",
}},
err: false,
},
}
}

Expand Down

0 comments on commit 098706a

Please sign in to comment.