@@ -32,6 +32,10 @@ func (p NoOpPolicyChecker) CheckPlanPolicy(_ string, _ string, _ string) (bool,
3232 return true , nil , nil
3333}
3434
35+ func (p NoOpPolicyChecker ) CheckDriftPolicy (SCMOrganisation string , SCMrepository string , projectname string ) (bool , error ) {
36+ return true , nil
37+ }
38+
3539func getAccessPolicyForOrganisation (p * DiggerHttpPolicyProvider ) (string , * http.Response , error ) {
3640 organisation := p .DiggerOrganisation
3741 u , err := url .Parse (p .DiggerHost )
@@ -84,6 +88,32 @@ func getPlanPolicyForOrganisation(p *DiggerHttpPolicyProvider) (string, *http.Re
8488 return string (body ), resp , nil
8589}
8690
91+ func getDriftPolicyForOrganisation (p * DiggerHttpPolicyProvider ) (string , * http.Response , error ) {
92+ organisation := p .DiggerOrganisation
93+ u , err := url .Parse (p .DiggerHost )
94+ if err != nil {
95+ log .Fatalf ("Not able to parse digger cloud url: %v" , err )
96+ }
97+ u .Path = "/orgs/" + organisation + "/drift-policy"
98+ req , err := http .NewRequest ("GET" , u .String (), nil )
99+ if err != nil {
100+ return "" , nil , err
101+ }
102+ req .Header .Add ("Authorization" , "Bearer " + p .AuthToken )
103+
104+ resp , err := p .HttpClient .Do (req )
105+ if err != nil {
106+ return "" , nil , err
107+ }
108+ defer resp .Body .Close ()
109+
110+ body , err := io .ReadAll (resp .Body )
111+ if err != nil {
112+ return "" , resp , nil
113+ }
114+ return string (body ), resp , nil
115+ }
116+
87117func getAccessPolicyForNamespace (p * DiggerHttpPolicyProvider , namespace string , projectName string ) (string , * http.Response , error ) {
88118 // fetch RBAC policies for project from Digger API
89119 u , err := url .Parse (p .DiggerHost )
@@ -200,6 +230,20 @@ func (p *DiggerHttpPolicyProvider) GetPlanPolicy(organisation string, repo strin
200230 }
201231}
202232
233+ func (p * DiggerHttpPolicyProvider ) GetDriftPolicy () (string , error ) {
234+ content , resp , err := getDriftPolicyForOrganisation (p )
235+ if err != nil {
236+ return "" , err
237+ }
238+ if resp .StatusCode == 200 {
239+ return content , nil
240+ } else if resp .StatusCode == 404 {
241+ return "" , nil
242+ } else {
243+ return "" , errors .New (fmt .Sprintf ("unexpected response while fetching organisation policy: %v, code %v" , content , resp .StatusCode ))
244+ }
245+ }
246+
203247func (p * DiggerHttpPolicyProvider ) GetOrganisation () string {
204248 return p .DiggerOrganisation
205249}
@@ -330,3 +374,52 @@ func (p DiggerPolicyChecker) CheckPlanPolicy(SCMrepository string, projectName s
330374
331375 return true , []string {}, nil
332376}
377+
378+ func (p DiggerPolicyChecker ) CheckDriftPolicy (SCMOrganisation string , SCMrepository string , projectName string ) (bool , error ) {
379+ // TODO: Get rid of organisation if its not needed
380+ //organisation := p.PolicyProvider.GetOrganisation()
381+ policy , err := p .PolicyProvider .GetDriftPolicy ()
382+ if err != nil {
383+ fmt .Printf ("Error while fetching drift policy: %v" , err )
384+ return false , err
385+ }
386+
387+ input := map [string ]interface {}{
388+ "organisation" : SCMOrganisation ,
389+ "project" : projectName ,
390+ }
391+
392+ if policy == "" {
393+ return true , nil
394+ }
395+
396+ ctx := context .Background ()
397+ fmt .Printf ("DEBUG: passing the following input policy: %v ||| text: %v" , input , policy )
398+ query , err := rego .New (
399+ rego .Query ("data.digger.allow" ),
400+ rego .Module ("digger" , policy ),
401+ ).PrepareForEval (ctx )
402+
403+ if err != nil {
404+ return false , err
405+ }
406+
407+ results , err := query .Eval (ctx , rego .EvalInput (input ))
408+ if len (results ) == 0 || len (results [0 ].Expressions ) == 0 {
409+ return false , fmt .Errorf ("no result found" )
410+ }
411+
412+ expressions := results [0 ].Expressions
413+
414+ for _ , expression := range expressions {
415+ decision , ok := expression .Value .(bool )
416+ if ! ok {
417+ return false , fmt .Errorf ("decision is not a boolean" )
418+ }
419+ if ! decision {
420+ return false , nil
421+ }
422+ }
423+
424+ return true , nil
425+ }
0 commit comments