-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduce repo policy provider (ee)
- Loading branch information
Showing
4 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: release ee cli | ||
on: | ||
release: | ||
branches: | ||
- 'go' | ||
types: [released] | ||
jobs: | ||
binary: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Download Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: 1.21.1 | ||
id: go | ||
|
||
- name: Check out repository | ||
uses: actions/checkout@v4 | ||
- name: Build | ||
run: | | ||
echo "Tag that is going to be used as digger version: ${{ github.event.release.tag_name }}" | ||
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-X digger/pkg/utils.version=${{ github.event.release.tag_name }}" -o digger ./ee/cli/cmd/digger | ||
- name: Publish linux-x64 exec to github | ||
id: upload-release-asset-linux-x64 | ||
uses: actions/upload-release-asset@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
upload_url: ${{ github.event.release.upload_url }} | ||
asset_path: 'digger' | ||
asset_name: digger-ee-cli-Linux-X64 | ||
asset_content_type: application/octet-stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package policy | ||
|
||
import ( | ||
"fmt" | ||
"github.com/diggerhq/digger/ee/cli/pkg/utils" | ||
"os" | ||
"path" | ||
) | ||
|
||
const DefaultAccessPolicy = ` | ||
package digger | ||
default allow = true | ||
allow = (count(input.planPolicyViolations) == 0) | ||
` | ||
|
||
type DiggerRepoPolicyProvider struct { | ||
ManagementRepoUrl string | ||
GitToken string | ||
} | ||
|
||
// GetPolicy fetches policy for particular project, if not found then it will fallback to org level policy | ||
func (p *DiggerRepoPolicyProvider) GetAccessPolicy(organisation string, repo string, projectName string) (string, error) { | ||
var policycontents []byte | ||
err := utils.CloneGitRepoAndDoAction(p.ManagementRepoUrl, "main", p.GitToken, func(basePath string) error { | ||
orgAccesspath := path.Join(basePath, "policies", "access.rego") | ||
//repoAccesspath := path.Join(basePath, "policies", repo, "access.rego") | ||
//projectAccessPath := path.Join(basePath, "policies", repo, projectName, "access.rego") | ||
var regoPath string | ||
if _, err := os.Stat(orgAccesspath); err != nil { | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("Could not find org level path") | ||
} else { | ||
return err | ||
} | ||
} else { | ||
regoPath = orgAccesspath | ||
} | ||
|
||
var err error | ||
policycontents, err = os.ReadFile(regoPath) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(policycontents), nil | ||
} | ||
|
||
func (p *DiggerRepoPolicyProvider) GetPlanPolicy(organisation string, repo string, projectName string) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func (p *DiggerRepoPolicyProvider) GetDriftPolicy() (string, error) { | ||
return "", nil | ||
|
||
} | ||
|
||
func (p *DiggerRepoPolicyProvider) GetOrganisation() string { | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"log" | ||
"os" | ||
) | ||
|
||
func createTempDir() string { | ||
tempDir, err := os.MkdirTemp("", "repo") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return tempDir | ||
} | ||
|
||
type action func(string) error | ||
|
||
func CloneGitRepoAndDoAction(repoUrl string, branch string, token string, action action) error { | ||
dir := createTempDir() | ||
cloneOptions := git.CloneOptions{ | ||
URL: repoUrl, | ||
ReferenceName: plumbing.NewBranchReferenceName(branch), | ||
Depth: 1, | ||
SingleBranch: true, | ||
} | ||
|
||
if token != "" { | ||
cloneOptions.Auth = &http.BasicAuth{ | ||
Username: "x-access-token", // anything except an empty string | ||
Password: token, | ||
} | ||
} | ||
|
||
_, err := git.PlainClone(dir, false, &cloneOptions) | ||
if err != nil { | ||
log.Printf("PlainClone error: %v\n", err) | ||
return err | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
err = action(dir) | ||
if err != nil { | ||
log.Printf("error performing action: %v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
} |