Skip to content

Commit 4989623

Browse files
Balijepalli Vamshi KrishnaBalijepalli Vamshi Krishna
authored andcommitted
support for passing logger
1 parent b9c9f53 commit 4989623

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (h Handler) Invoke(ctx context.Context, req []byte) ([]byte, error) {
128128
inputYaml = httpRequest.Body
129129
}
130130

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

133133
if err != nil {
134134
response = events.APIGatewayProxyResponse{

remediation/workflow/hardenrunner/addaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func AddAction(inputYaml, action string, pinActions, pinToImmutable bool, skipCo
5151
}
5252

5353
if updated && pinActions {
54-
out, _, err = pin.PinAction(action, out, nil, pinToImmutable, nil)
54+
out, _, err = pin.PinAction(action, out, nil, pinToImmutable, nil, nil)
5555
if err != nil {
5656
return out, updated, err
5757
}

remediation/workflow/pin/pinactions.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ package pin
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"os"
87
"path/filepath"
98
"regexp"
109
"strings"
1110

1211
"github.com/google/go-github/v40/github"
12+
"github.com/sirupsen/logrus"
1313
metadata "github.com/step-security/secure-repo/remediation/workflow/metadata"
1414
"golang.org/x/oauth2"
1515
"gopkg.in/yaml.v3"
1616
)
1717

18-
func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool, actionCommitMap map[string]string) (string, bool, error) {
18+
type StepSecurityAppLogger struct {
19+
RequestID string `json:"request_id,omitempty"`
20+
*logrus.Logger
21+
}
22+
23+
func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool, actionCommitMap map[string]string, logger *StepSecurityAppLogger) (string, bool, error) {
1924
workflow := metadata.Workflow{}
2025
updated := false
2126
err := yaml.Unmarshal([]byte(inputYaml), &workflow)
@@ -30,7 +35,7 @@ func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool,
3035
for _, step := range job.Steps {
3136
if len(step.Uses) > 0 {
3237
localUpdated := false
33-
out, localUpdated, err = PinAction(step.Uses, out, exemptedActions, pinToImmutable, actionCommitMap)
38+
out, localUpdated, err = PinAction(step.Uses, out, exemptedActions, pinToImmutable, actionCommitMap, logger)
3439
if err != nil {
3540
return out, updated, err
3641
}
@@ -42,7 +47,7 @@ func PinActions(inputYaml string, exemptedActions []string, pinToImmutable bool,
4247
return out, updated, nil
4348
}
4449

45-
func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutable bool, actionCommitMap map[string]string) (string, bool, error) {
50+
func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutable bool, actionCommitMap map[string]string, logger *StepSecurityAppLogger) (string, bool, error) {
4651

4752
updated := false
4853
if !strings.Contains(action, "@") || strings.HasPrefix(action, "docker://") {
@@ -68,9 +73,17 @@ func PinAction(action, inputYaml string, exemptedActions []string, pinToImmutabl
6873
PAT := os.Getenv("SECURE_REPO_PAT")
6974
if PAT == "" {
7075
PAT = os.Getenv("PAT")
71-
log.Println("SECURE_REPO_PAT is not set, using PAT")
76+
if logger != nil {
77+
logger.Logf(logrus.InfoLevel, "SECURE_REPO_PAT is not set, using PAT")
78+
} else {
79+
logrus.Info("SECURE_REPO_PAT is not set, using PAT")
80+
}
7281
} else {
73-
log.Println("SECURE_REPO_PAT is set")
82+
if logger != nil {
83+
logger.Logf(logrus.InfoLevel, "SECURE_REPO_PAT is set")
84+
} else {
85+
logrus.Info("SECURE_REPO_PAT is set")
86+
}
7487
}
7588

7689
ctx := context.Background()

remediation/workflow/pin/pinactions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func TestPinActions(t *testing.T) {
333333
}
334334
}
335335

336-
output, gotUpdated, err = PinActions(string(input), tt.exemptedActions, tt.pinToImmutable, actionCommitMap)
336+
output, gotUpdated, err = PinActions(string(input), tt.exemptedActions, tt.pinToImmutable, actionCommitMap, nil)
337337
if tt.wantUpdated != gotUpdated {
338338
t.Errorf("test failed wantUpdated %v did not match gotUpdated %v", tt.wantUpdated, gotUpdated)
339339
}

remediation/workflow/secureworkflow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
HardenRunnerActionName = "Harden Runner"
1818
)
1919

20-
func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc dynamodbiface.DynamoDBAPI, params ...interface{}) (*permissions.SecureWorkflowReponse, error) {
20+
func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc dynamodbiface.DynamoDBAPI, logger *pin.StepSecurityAppLogger, params ...interface{}) (*permissions.SecureWorkflowReponse, error) {
2121
pinActions, addHardenRunner, addPermissions, addProjectComment, replaceMaintainedActions := true, true, true, true, false
2222
pinnedActions, addedHardenRunner, addedPermissions, replacedMaintainedActions := false, false, false, false
2323
ignoreMissingKBs := false
@@ -148,7 +148,7 @@ func SecureWorkflow(queryStringParams map[string]string, inputYaml string, svc d
148148
log.Printf("Pinning GitHub Actions")
149149
}
150150
pinnedAction, pinnedDocker := false, false
151-
secureWorkflowReponse.FinalOutput, pinnedAction, err = pin.PinActions(secureWorkflowReponse.FinalOutput, exemptedActions, pinToImmutable, actionCommitMap)
151+
secureWorkflowReponse.FinalOutput, pinnedAction, err = pin.PinActions(secureWorkflowReponse.FinalOutput, exemptedActions, pinToImmutable, actionCommitMap, logger)
152152
if err != nil {
153153
if enableLogging {
154154
log.Printf("Error pinning actions: %v", err)

remediation/workflow/secureworkflow_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ func TestSecureWorkflow(t *testing.T) {
266266
if err != nil {
267267
t.Errorf("unable to load the file %s", err)
268268
}
269-
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, []string{"actions/*"}, false, actionMap)
269+
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, nil, []string{"actions/*"}, false, actionMap)
270270
} else {
271-
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
271+
output, err = SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, nil)
272272
}
273273

274274
if test.wantError {
@@ -369,7 +369,7 @@ func TestSecureWorkflowContainerJob(t *testing.T) {
369369
queryParams["skipHardenRunnerForContainers"] = "true"
370370
queryParams["addProjectComment"] = "false"
371371

372-
output, err := SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
372+
output, err := SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, nil)
373373

374374
if err != nil {
375375
t.Errorf("Error not expected")
@@ -474,7 +474,7 @@ func TestSecureWorkflowEmptyPermissions(t *testing.T) {
474474
queryParams["addEmptyTopLevelPermissions"] = "true"
475475
queryParams["addProjectComment"] = "false"
476476

477-
output, err := SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{})
477+
output, err := SecureWorkflow(queryParams, string(input), &mockDynamoDBClient{}, nil)
478478

479479
if err != nil {
480480
t.Errorf("Error not expected")

0 commit comments

Comments
 (0)