@@ -8,28 +8,29 @@ import (
88 "github.com/open-policy-agent/opa/rego"
99 "io"
1010 "net/http"
11- "strings"
1211)
1312
1413type PolicyProvider interface {
15- GetPolicy (organisation string , namespace string , projectname string ) (string , error )
14+ GetPolicy (organisation string , repository string , projectname string ) (string , error )
15+ GetOrganisation () string
1616}
1717
1818type DiggerHttpPolicyProvider struct {
19- DiggerHost string
20- AuthToken string
21- HttpClient * http.Client
19+ DiggerHost string
20+ DiggerOrganisation string
21+ AuthToken string
22+ HttpClient * http.Client
2223}
2324
2425type NoOpPolicyChecker struct {
2526}
2627
27- func (p NoOpPolicyChecker ) Check (_ string , _ string , _ string , _ string , _ string ) (bool , error ) {
28+ func (p NoOpPolicyChecker ) Check (_ ci. CIService , _ string , _ string , _ string , _ string , _ string ) (bool , error ) {
2829 return true , nil
2930}
3031
31- func (p * DiggerHttpPolicyProvider ) getPolicyForOrganisation ( organisation string ) (string , * http.Response , error ) {
32-
32+ func getPolicyForOrganisation (p * DiggerHttpPolicyProvider ) (string , * http.Response , error ) {
33+ organisation := p . DiggerOrganisation
3334 req , err := http .NewRequest ("GET" , p .DiggerHost + "/orgs/" + organisation + "/access-policy" , nil )
3435 if err != nil {
3536 return "" , nil , err
@@ -49,10 +50,8 @@ func (p *DiggerHttpPolicyProvider) getPolicyForOrganisation(organisation string)
4950 return string (body ), resp , nil
5051}
5152
52- func (p * DiggerHttpPolicyProvider ) getPolicyForNamespace (namespace string , projectName string ) (string , * http.Response , error ) {
53-
54- // fetch RBAC policies for projectfrom Digger API
55- namespace = strings .ReplaceAll (namespace , "/" , "-" )
53+ func getPolicyForNamespace (p * DiggerHttpPolicyProvider , namespace string , projectName string ) (string , * http.Response , error ) {
54+ // fetch RBAC policies for project from Digger API
5655 req , err := http .NewRequest ("GET" , p .DiggerHost + "/repos/" + namespace + "/projects/" + projectName + "/access-policy" , nil )
5756
5857 if err != nil {
@@ -75,15 +74,17 @@ func (p *DiggerHttpPolicyProvider) getPolicyForNamespace(namespace string, proje
7574}
7675
7776// GetPolicy fetches policy for particular project, if not found then it will fallback to org level policy
78- func (p * DiggerHttpPolicyProvider ) GetPolicy (organisation string , namespace string , projectName string ) (string , error ) {
79- content , resp , err := p .getPolicyForNamespace (namespace , projectName )
77+ func (p * DiggerHttpPolicyProvider ) GetPolicy (organisation string , repo string , projectName string ) (string , error ) {
78+ namespace := fmt .Sprintf ("%v-%v" , organisation , repo )
79+ content , resp , err := getPolicyForNamespace (p , namespace , projectName )
8080 if err != nil {
8181 return "" , err
8282 }
83- if resp .StatusCode == 200 {
83+
84+ if resp .StatusCode == 200 && content != "" {
8485 return content , nil
8586 } else if resp .StatusCode == 404 {
86- content , resp , err := p . getPolicyForOrganisation (organisation )
87+ content , resp , err := getPolicyForOrganisation (p )
8788 if err != nil {
8889 return "" , err
8990 }
@@ -99,15 +100,18 @@ func (p *DiggerHttpPolicyProvider) GetPolicy(organisation string, namespace stri
99100 }
100101}
101102
103+ func (p * DiggerHttpPolicyProvider ) GetOrganisation () string {
104+ return p .DiggerOrganisation
105+ }
106+
102107type DiggerPolicyChecker struct {
103108 PolicyProvider PolicyProvider
104- ciService ci.CIService
105109}
106110
107- func (p DiggerPolicyChecker ) Check (organisation string , namespace string , projectName string , command string , requestedBy string ) (bool , error ) {
108- policy , err := p .PolicyProvider .GetPolicy ( organisation , namespace , projectName )
109-
110- teams , err := p . ciService .GetUserTeams (organisation , requestedBy )
111+ func (p DiggerPolicyChecker ) Check (ciService ci. CIService , SCMOrganisation string , SCMrepository string , projectName string , command string , requestedBy string ) (bool , error ) {
112+ organisation := p .PolicyProvider .GetOrganisation ( )
113+ policy , err := p . PolicyProvider . GetPolicy ( organisation , SCMrepository , projectName )
114+ teams , err := ciService .GetUserTeams (SCMOrganisation , requestedBy )
111115 if err != nil {
112116 fmt .Printf ("Error while fetching user teams for CI service: %v" , err )
113117 return false , err
0 commit comments