Skip to content

Commit adaff53

Browse files
Merge pull request #1400 from step-security/remediation
Update folder structure
2 parents 31fbcd7 + a212a08 commit adaff53

29 files changed

+970
-148
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
with:
3939
go-version: 1.17
4040
- name: Run coverage
41-
run: go test -race -coverprofile=coverage.txt -covermode=atomic
41+
run: go test ./... -coverpkg=./... -race -coverprofile=coverage.txt -covermode=atomic
4242
env:
4343
PAT: ${{ secrets.GITHUB_TOKEN }}
4444
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ require (
66
github.com/asottile/dockerfile v3.1.0+incompatible
77
github.com/aws/aws-lambda-go v1.30.0
88
github.com/aws/aws-sdk-go v1.43.45
9+
github.com/paulvollmer/dependabot-config-go v0.1.1
10+
gopkg.in/yaml.v2 v2.4.0
911
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
1012
)
1113

@@ -29,7 +31,6 @@ require (
2931
github.com/moby/buildkit v0.10.3 // indirect
3032
github.com/opencontainers/go-digest v1.0.0 // indirect
3133
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
32-
github.com/paulvollmer/dependabot-config-go v0.1.1 // indirect
3334
github.com/pkg/errors v0.9.1 // indirect
3435
github.com/sirupsen/logrus v1.8.1 // indirect
3536
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
@@ -38,7 +39,6 @@ require (
3839
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
3940
google.golang.org/appengine v1.6.7 // indirect
4041
google.golang.org/protobuf v1.28.0 // indirect
41-
gopkg.in/yaml.v2 v2.4.0 // indirect
4242
)
4343

4444
require (

go.sum

Lines changed: 819 additions & 25 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"github.com/aws/aws-lambda-go/lambda"
1212
"github.com/aws/aws-sdk-go/aws/session"
1313
"github.com/aws/aws-sdk-go/service/dynamodb"
14+
"github.com/step-security/secure-workflows/remediation/dependabot"
15+
"github.com/step-security/secure-workflows/remediation/docker"
16+
"github.com/step-security/secure-workflows/remediation/secrets"
17+
"github.com/step-security/secure-workflows/remediation/workflow"
18+
"github.com/step-security/secure-workflows/remediation/workflow/permissions"
1419
)
1520

1621
type Handler struct {
@@ -42,7 +47,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
4247
if strings.Contains(httpRequest.RawPath, "/secrets") {
4348
if httpRequest.RequestContext.HTTP.Method == "GET" {
4449
authHeader := httpRequest.Headers["authorization"]
45-
githubWorkflowSecrets, err := GetSecrets(httpRequest.QueryStringParameters, authHeader, dynamoDbSvc)
50+
githubWorkflowSecrets, err := secrets.GetSecrets(httpRequest.QueryStringParameters, authHeader, dynamoDbSvc)
4651
if err != nil {
4752
response = events.APIGatewayProxyResponse{
4853
StatusCode: http.StatusInternalServerError,
@@ -58,7 +63,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
5863

5964
} else if httpRequest.RequestContext.HTTP.Method == "PUT" {
6065
authHeader := httpRequest.Headers["authorization"]
61-
githubWorkflowSecrets, err := InitSecrets(httpRequest.Body, authHeader, dynamoDbSvc)
66+
githubWorkflowSecrets, err := secrets.InitSecrets(httpRequest.Body, authHeader, dynamoDbSvc)
6267
if err != nil {
6368
response = events.APIGatewayProxyResponse{
6469
StatusCode: http.StatusInternalServerError,
@@ -73,7 +78,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
7378
}
7479

7580
} else if httpRequest.RequestContext.HTTP.Method == "POST" {
76-
err := SetSecrets(httpRequest.Body, dynamoDbSvc)
81+
err := secrets.SetSecrets(httpRequest.Body, dynamoDbSvc)
7782
if err != nil {
7883
response = events.APIGatewayProxyResponse{
7984
StatusCode: http.StatusInternalServerError,
@@ -86,7 +91,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
8691
}
8792
} else if httpRequest.RequestContext.HTTP.Method == "DELETE" {
8893
authHeader := httpRequest.Headers["authorization"]
89-
err := DeleteSecrets(authHeader, dynamoDbSvc)
94+
err := secrets.DeleteSecrets(authHeader, dynamoDbSvc)
9095
if err != nil {
9196
response = events.APIGatewayProxyResponse{
9297
StatusCode: http.StatusInternalServerError,
@@ -107,9 +112,9 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
107112
// if owner is set, assuming that repo, path are also set
108113
// get the workflow using API
109114
if _, ok := queryStringParams["owner"]; ok {
110-
inputYaml, err = GetGitHubWorkflowContents(httpRequest.QueryStringParameters)
115+
inputYaml, err = workflow.GetGitHubWorkflowContents(httpRequest.QueryStringParameters)
111116
if err != nil {
112-
fixResponse := &SecureWorkflowReponse{WorkflowFetchError: true, HasErrors: true}
117+
fixResponse := &permissions.SecureWorkflowReponse{WorkflowFetchError: true, HasErrors: true}
113118
output, _ := json.Marshal(fixResponse)
114119
response = events.APIGatewayProxyResponse{
115120
StatusCode: http.StatusOK,
@@ -123,7 +128,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
123128
inputYaml = httpRequest.Body
124129
}
125130

126-
fixResponse, err := SecureWorkflow(httpRequest.QueryStringParameters, inputYaml, dynamoDbSvc)
131+
fixResponse, err := workflow.SecureWorkflow(httpRequest.QueryStringParameters, inputYaml, dynamoDbSvc)
127132

128133
if err != nil {
129134
response = events.APIGatewayProxyResponse{
@@ -148,9 +153,9 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
148153
// if owner is set, assuming that repo, path are also set
149154
// get the dockerfile using API
150155
if _, ok := queryStringParams["owner"]; ok {
151-
dockerFile, err = GetGitHubWorkflowContents(httpRequest.QueryStringParameters)
156+
dockerFile, err = workflow.GetGitHubWorkflowContents(httpRequest.QueryStringParameters)
152157
if err != nil {
153-
fixResponse := &SecureDockerfileResponse{DockerfileFetchError: true}
158+
fixResponse := &docker.SecureDockerfileResponse{DockerfileFetchError: true}
154159
output, _ := json.Marshal(fixResponse)
155160
response = events.APIGatewayProxyResponse{
156161
StatusCode: http.StatusOK,
@@ -164,7 +169,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
164169
dockerFile = httpRequest.Body
165170
}
166171

167-
fixResponse, err := SecureDockerFile(dockerFile)
172+
fixResponse, err := docker.SecureDockerFile(dockerFile)
168173
if err != nil {
169174
response = events.APIGatewayProxyResponse{
170175
StatusCode: http.StatusInternalServerError,
@@ -186,7 +191,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
186191
updateDependabotConfigRequest := ""
187192
updateDependabotConfigRequest = httpRequest.Body
188193

189-
fixResponse, err := UpdateDependabotConfig(updateDependabotConfigRequest)
194+
fixResponse, err := dependabot.UpdateDependabotConfig(updateDependabotConfigRequest)
190195
if err != nil {
191196
response = events.APIGatewayProxyResponse{
192197
StatusCode: http.StatusInternalServerError,

dependabotconfig.go renamed to remediation/dependabot/dependabotconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package dependabot
22

33
import (
44
"bufio"

dependabotconfig_test.go renamed to remediation/dependabot/dependabotconfig_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package dependabot
22

33
import (
44
"encoding/json"
@@ -10,8 +10,8 @@ import (
1010

1111
func TestConfigDependabotFile(t *testing.T) {
1212

13-
const inputDirectory = "./testfiles/dependabotfiles/input"
14-
const outputDirectory = "./testfiles/dependabotfiles/output"
13+
const inputDirectory = "../../testfiles/dependabotfiles/input"
14+
const outputDirectory = "../../testfiles/dependabotfiles/output"
1515

1616
tests := []struct {
1717
fileName string

securedockerfile.go renamed to remediation/docker/securedockerfile.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package main
1+
package docker
22

33
import (
44
"fmt"
5+
"net/http"
56
"strings"
67

78
"github.com/asottile/dockerfile"
@@ -10,6 +11,8 @@ import (
1011
"github.com/google/go-containerregistry/pkg/v1/remote"
1112
)
1213

14+
var Tr http.RoundTripper = remote.DefaultTransport
15+
1316
type SecureDockerfileResponse struct {
1417
OriginalInput string
1518
FinalOutput string

securedockerfile_test.go renamed to remediation/docker/securedockerfile_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package docker
22

33
import (
44
"io/ioutil"
@@ -9,12 +9,12 @@ import (
99
"github.com/jarcoal/httpmock"
1010
)
1111

12-
var resp = httpmock.File("./testfiles/dockerfiles/response.json").String()
12+
var resp = httpmock.File("../../testfiles/dockerfiles/response.json").String()
1313

1414
func TestSecureDockerFile(t *testing.T) {
1515

16-
const inputDirectory = "./testfiles/dockerfiles/input"
17-
const outputDirectory = "./testfiles/dockerfiles/output"
16+
const inputDirectory = "../../testfiles/dockerfiles/input"
17+
const outputDirectory = "../../testfiles/dockerfiles/output"
1818
// NOTE: http mocking is not working,
1919
// need to investigate this issue
2020
httpmock.Activate()

secrets.go renamed to remediation/secrets/secrets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package secrets
22

33
import (
44
"context"

secrets_test.go renamed to remediation/secrets/secrets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package secrets
22

33
import (
44
"reflect"

addaction.go renamed to remediation/workflow/hardenrunner/addaction.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
package main
1+
package hardenrunner
22

33
import (
44
"fmt"
55
"strings"
66

7+
metadata "github.com/step-security/secure-workflows/remediation/workflow/metadata"
8+
"github.com/step-security/secure-workflows/remediation/workflow/permissions"
79
"gopkg.in/yaml.v3"
810
)
911

12+
const (
13+
HardenRunnerActionPath = "step-security/harden-runner"
14+
HardenRunnerActionName = "Harden Runner"
15+
)
16+
1017
func AddAction(inputYaml, action string) (string, bool, error) {
11-
workflow := Workflow{}
18+
workflow := metadata.Workflow{}
1219
updated := false
1320
err := yaml.Unmarshal([]byte(inputYaml), &workflow)
1421
if err != nil {
@@ -18,7 +25,7 @@ func AddAction(inputYaml, action string) (string, bool, error) {
1825

1926
for jobName, job := range workflow.Jobs {
2027
// Skip adding action for reusable jobs
21-
if IsCallingReusableWorkflow(job) {
28+
if metadata.IsCallingReusableWorkflow(job) {
2229
continue
2330
}
2431
alreadyPresent := false
@@ -49,9 +56,9 @@ func addAction(inputYaml, jobName, action string) (string, error) {
4956
return "", fmt.Errorf("unable to parse yaml %v", err)
5057
}
5158

52-
jobNode := iterateNode(&t, jobName, "!!map", 0)
59+
jobNode := permissions.IterateNode(&t, jobName, "!!map", 0)
5360

54-
jobNode = iterateNode(&t, "steps", "!!seq", jobNode.Line)
61+
jobNode = permissions.IterateNode(&t, "steps", "!!seq", jobNode.Line)
5562

5663
if jobNode == nil {
5764
return "", fmt.Errorf("jobName %s not found in the input yaml", jobName)

addaction_test.go renamed to remediation/workflow/hardenrunner/addaction_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package hardenrunner
22

33
import (
44
"io/ioutil"
@@ -11,8 +11,8 @@ func TestAddAction(t *testing.T) {
1111
inputYaml string
1212
action string
1313
}
14-
const inputDirectory = "./testfiles/addaction/input"
15-
const outputDirectory = "./testfiles/addaction/output"
14+
const inputDirectory = "../../../testfiles/addaction/input"
15+
const outputDirectory = "../../../testfiles/addaction/output"
1616
tests := []struct {
1717
name string
1818
args args

issue.go renamed to remediation/workflow/issue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package workflow
22

33
import (
44
"context"
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/google/go-github/v40/github"
10+
metadata "github.com/step-security/secure-workflows/remediation/workflow/metadata"
1011
"golang.org/x/oauth2"
1112
)
1213

@@ -22,7 +23,7 @@ func CreateIssue(Action string) (int, error) {
2223
// is action
2324
if len(Action) > 0 {
2425
// is kb not found
25-
_, err := GetActionKnowledgeBase(Action)
26+
_, err := metadata.GetActionKnowledgeBase(Action)
2627

2728
if err != nil {
2829
// does issue already exist?

issue_test.go renamed to remediation/workflow/issue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package workflow
22

33
import (
44
"os"

metadata.go renamed to remediation/workflow/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package workflow
22

33
import (
44
"strings"

actionmetadata.go renamed to remediation/workflow/metadata/actionmetadata.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package metadata
22

33
import (
44
"errors"
@@ -156,11 +156,10 @@ func (p *Permissions) UnmarshalYAML(unmarshal func(interface{}) error) error {
156156

157157
func GetActionKnowledgeBase(action string) (*ActionMetadata, error) {
158158
kbFolder := os.Getenv("KBFolder")
159-
160159
// converting actionKey to lowercase to fix ISSUE#286
161160
action = strings.ToLower(action)
162161
if kbFolder == "" {
163-
kbFolder = "knowledge-base/actions"
162+
kbFolder = "../../knowledge-base/actions"
164163
}
165164

166165
input, err := ioutil.ReadFile(path.Join(kbFolder, action, "action-security.yml"))

actionmetadata_test.go renamed to remediation/workflow/metadata/actionmetadata_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package metadata
22

33
import (
44
"context"
@@ -20,7 +20,7 @@ func TestKnowledgeBase(t *testing.T) {
2020
kbFolder := os.Getenv("KBFolder")
2121

2222
if kbFolder == "" {
23-
kbFolder = "knowledge-base/actions"
23+
kbFolder = "../../../knowledge-base/actions"
2424
}
2525

2626
lintIssues := []string{}
@@ -181,8 +181,8 @@ func TestKnowledgeBase(t *testing.T) {
181181

182182
func doesActionRepoExist(filePath string) bool {
183183
splitOnSlash := strings.Split(filePath, "/")
184-
owner := splitOnSlash[2]
185-
repo := splitOnSlash[3]
184+
owner := splitOnSlash[5]
185+
repo := splitOnSlash[6]
186186

187187
PAT := os.Getenv("PAT")
188188
if len(PAT) == 0 {
@@ -207,13 +207,13 @@ func doesActionRepoExist(filePath string) bool {
207207
ref.Ref = *branch
208208

209209
// does the path to folder is correct for action repository
210-
if len(splitOnSlash) > 5 {
211-
folder := strings.Join(splitOnSlash[4:len(splitOnSlash)-1], "/")
210+
if len(splitOnSlash) > 8 {
211+
folder := strings.Join(splitOnSlash[7:len(splitOnSlash)-1], "/")
212212
folder += "/action.yml"
213213
_, _, _, err = client.Repositories.GetContents(context.Background(), owner, repo, folder, &ref)
214214

215215
if err != nil {
216-
folder := strings.Join(splitOnSlash[4:len(splitOnSlash)-1], "/")
216+
folder := strings.Join(splitOnSlash[7:len(splitOnSlash)-1], "/")
217217
folder += "/action.yaml" // try out .yaml extension as well
218218
_, _, _, err = client.Repositories.GetContents(context.Background(), owner, repo, folder, &ref)
219219

metadata_test.go renamed to remediation/workflow/metadata_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package main
1+
package workflow
22

33
import (
44
"io/ioutil"
55

66
"github.com/aws/aws-sdk-go/service/dynamodb"
77
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
88
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
9+
metadata "github.com/step-security/secure-workflows/remediation/workflow/metadata"
910
"gopkg.in/yaml.v3"
1011
)
1112

@@ -21,13 +22,13 @@ func (m *mockDynamoDBClient) PutItem(input *dynamodb.PutItemInput) (*dynamodb.Pu
2122

2223
func (m *mockDynamoDBClient) Scan(input *dynamodb.ScanInput) (*dynamodb.ScanOutput, error) {
2324

24-
actionPermissionsYaml, err := ioutil.ReadFile("./testfiles/action-permissions.yml")
25+
actionPermissionsYaml, err := ioutil.ReadFile("../../testfiles/action-permissions.yml")
2526

2627
if err != nil {
2728
return nil, err
2829
}
2930

30-
actionPermissions := ActionPermissions{}
31+
actionPermissions := metadata.ActionPermissions{}
3132

3233
err = yaml.Unmarshal(actionPermissionsYaml, &actionPermissions)
3334

0 commit comments

Comments
 (0)