Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload validation results to s3 #453

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ build:
docs:
go run ./docs/

.PHONY: validate
validate:
dagger call validate \
--one-password-service-account env:OP_SERVICE_ACCOUNT \
--progress plain

.PHONE: release
release:
dagger call release \
Expand Down
4 changes: 4 additions & 0 deletions dagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "replicated",
"sdk": "go",
"dependencies": [
{
"name": "aws-sdk",
"source": "github.com/ernesto27/daggerverse/aws-sdk@4921d3afbdd99bc24a214d9a88c6ed8b50adca6c"
},
{
"name": "goreleaser",
"source": "github.com/developer-guy/excoriate-daggerverse/goreleaser@b4bc20faec104b7f45c9a429b20f7d39d1531b79"
Expand Down
4 changes: 2 additions & 2 deletions dagger/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func validateCompatibility(

// +defaultPath="./"
source *dagger.Directory,
) error {
return nil
) (bool, map[string]Logs, error) {
return true, map[string]Logs{}, nil
}
18 changes: 14 additions & 4 deletions dagger/functionality.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ func validateFunctionality(

// +defaultPath="./"
source *dagger.Directory,
) error {
) (bool, map[string]Logs, error) {
goModCache := dag.CacheVolume("replicated-go-mod-122")
goBuildCache := dag.CacheVolume("replicated-go-build-121")

checkLogs := map[string]Logs{}

// unit tests
unitTest := dag.Container().
From("golang:1.22").
Expand All @@ -25,10 +27,18 @@ func validateFunctionality(
WithEnvVariable("GOCACHE", "/go/build-cache").
With(CacheBustingExec([]string{"make", "test-unit"}))

_, err := unitTest.Stderr(ctx)
unitTestStdout, err := unitTest.Stdout(ctx)
if err != nil {
return false, nil, err
}
unitTestStderr, err := unitTest.Stderr(ctx)
if err != nil {
return err
return false, nil, err
}
checkLogs["unit-tests"] = Logs{
Stdout: unitTestStdout,
Stderr: unitTestStderr,
}

return nil
return true, checkLogs, nil
}
2 changes: 2 additions & 0 deletions dagger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require (
google.golang.org/grpc v1.65.0
)

require github.com/aws/aws-sdk-go v1.55.5 // indirect

require (
github.com/Masterminds/semver v1.5.0
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions dagger/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3Q
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
4 changes: 2 additions & 2 deletions dagger/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func validatePerformance(

// +defaultPath="./"
source *dagger.Directory,
) error {
return nil
) (bool, map[string]Logs, error) {
return true, map[string]Logs{}, nil
}
18 changes: 14 additions & 4 deletions dagger/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ func validateSecurity(

// +defaultPath="./"
source *dagger.Directory,
) error {
) (bool, map[string]Logs, error) {
goModCache := dag.CacheVolume("replicated-go-mod-122")
goBuildCache := dag.CacheVolume("replicated-go-build-121")

checkLogs := map[string]Logs{}

// run semgrep
semgrep := dag.Container().
From("returntocorp/semgrep").
Expand All @@ -25,10 +27,18 @@ func validateSecurity(
WithEnvVariable("GOCACHE", "/go/build-cache").
With(CacheBustingExec([]string{"semgrep", "scan", "--config=p/golang", "."}))

_, err := semgrep.Stderr(ctx)
semgrepStdout, err := semgrep.Stdout(ctx)
if err != nil {
return false, nil, err
}
semgrepStderr, err := semgrep.Stderr(ctx)
if err != nil {
return err
return false, nil, err
}
checkLogs["semgrep"] = Logs{
Stdout: semgrepStdout,
Stderr: semgrepStderr,
}

return nil
return true, checkLogs, nil
}
150 changes: 146 additions & 4 deletions dagger/validate.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,173 @@
package main

import (
"bytes"
"context"
"dagger/replicated/internal/dagger"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

type Logs struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
}

type ValidationResult struct {
IsSecurityPass bool `json:"isSecurityPass"`
SecurityLogs map[string]Logs `json:"securityLogs"`

IsFunctionalityPass bool `json:"isFunctionalityPass"`
FunctionalityLogs map[string]Logs `json:"functionalityLogs"`

IsCompatibilityPass bool `json:"isCompatibilityPass"`
CompatibilityLogs map[string]Logs `json:"compatibilityLogs"`

IsPerformancePass bool `json:"isPerformancePass"`
PerformanceLogs map[string]Logs `json:"performanceLogs"`
}

func (r *Replicated) Validate(
ctx context.Context,

// +defaultPath="./"
source *dagger.Directory,

// +optional
onePasswordServiceAccount *dagger.Secret,
) error {
if err := validateSecurity(ctx, source); err != nil {
validationResult := ValidationResult{}

securityOK, securityLogs, err := validateSecurity(ctx, source)
if err != nil {
return err
}
validationResult.IsSecurityPass = securityOK
validationResult.SecurityLogs = securityLogs

functionalityOK, functionalityLogs, err := validateFunctionality(ctx, source)
if err != nil {
return err
}
validationResult.IsFunctionalityPass = functionalityOK
validationResult.FunctionalityLogs = functionalityLogs

compatibilityOK, copmfunctionalityLogs, err := validateCompatibility(ctx, source)
if err != nil {
return err
}
validationResult.IsCompatibilityPass = compatibilityOK
validationResult.CompatibilityLogs = copmfunctionalityLogs

performanceOK, performanceLogs, err := validatePerformance(ctx, source)
if err != nil {
return err
}
validationResult.IsPerformancePass = performanceOK
validationResult.PerformanceLogs = performanceLogs

if onePasswordServiceAccount != nil {
container := dag.Container().
From("alpine/git:latest").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
With(CacheBustingExec([]string{"git", "status", "--porcelain"}))

gitStatusOutput, err := container.Stdout(ctx)
if err != nil {
return err
}

gitTreeClean := len(strings.TrimSpace(gitStatusOutput)) == 0

container = dag.Container().
From("alpine/git:latest").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
With(CacheBustingExec([]string{"git", "rev-parse", "HEAD"}))

commit, err := container.Stdout(ctx)
if err != nil {
return err
}
commit = strings.TrimSpace(commit)

if gitTreeClean {
if err := uploadValidationResult(ctx, onePasswordServiceAccount, validationResult, commit); err != nil {
return err
}
}
}

return nil
}

func uploadValidationResult(ctx context.Context, onePasswordServiceAccount *dagger.Secret, validationResult ValidationResult, commit string) error {
b, err := json.Marshal(validationResult)
if err != nil {
return err
}

if err := validateFunctionality(ctx, source); err != nil {
// i think i need to create a new container to write b to a dagger.File?
f := dag.Directory().
WithNewFile("validation-result.json", string(b)).
File("validation-result.json")
f = f.WithName(commit)
accessKeyID, err := dag.Onepassword().FindSecret(
onePasswordServiceAccount,
"Developer Automation",
"S3 Workflow Validation",
"access_key_id").Plaintext(ctx)
if err != nil {
return err
}

if err := validateCompatibility(ctx, source); err != nil {
secretAccessKey, err := dag.Onepassword().FindSecret(
onePasswordServiceAccount,
"Developer Automation",
"S3 Workflow Validation",
"secret_access_key").Plaintext(ctx)
if err != nil {
return err
}

if err := validatePerformance(ctx, source); err != nil {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""),
}))

svc := s3.New(sess)
f.Export(ctx, commit)

readFile, err := os.ReadFile(commit)
if err != nil {
return err
}

_, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String("workflow-validation-results"),
Body: bytes.NewReader(readFile),
Key: aws.String(commit),
ContentDisposition: aws.String("attachment"),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == request.CanceledErrorCode {
fmt.Fprintf(os.Stderr, "upload canceled due to timeout, %v\n", err)
return err
} else {
fmt.Fprintf(os.Stderr, "failed to upload object, %v\n", err)
return err
}
}

return nil
}
Loading