-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: record token details to audit log
- Loading branch information
1 parent
c300bb3
commit d0a7a7b
Showing
4 changed files
with
122 additions
and
1 deletion.
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
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,30 @@ | ||
package vendor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jamestelfer/chinmina-bridge/internal/audit" | ||
"github.com/jamestelfer/chinmina-bridge/internal/jwt" | ||
) | ||
|
||
// Auditor is a function that wraps a PipelineTokenVendor and records the result | ||
// of vending a token to the audit log. | ||
func Auditor(vendor PipelineTokenVendor) PipelineTokenVendor { | ||
return func(ctx context.Context, claims jwt.BuildkiteClaims, repo string) (*PipelineRepositoryToken, error) { | ||
token, err := vendor(ctx, claims, repo) | ||
|
||
entry := audit.Log(ctx) | ||
if err != nil { | ||
entry.Error = fmt.Sprintf("vendor failure: %v", err) | ||
} else if token == nil { | ||
entry.Error = "repository mismatch, no token vended" | ||
} else { | ||
entry.Repositories = []string{token.RepositoryURL} | ||
entry.Permissions = []string{"contents:read"} | ||
entry.ExpirySecs = token.Expiry.Unix() | ||
} | ||
|
||
return token, err | ||
} | ||
} |
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,82 @@ | ||
package vendor_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jamestelfer/chinmina-bridge/internal/audit" | ||
"github.com/jamestelfer/chinmina-bridge/internal/jwt" | ||
"github.com/jamestelfer/chinmina-bridge/internal/vendor" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuditor_Success(t *testing.T) { | ||
successfulVendor := func(ctx context.Context, claims jwt.BuildkiteClaims, repo string) (*vendor.PipelineRepositoryToken, error) { | ||
return &vendor.PipelineRepositoryToken{ | ||
RepositoryURL: "https://example.com/repo", | ||
Expiry: time.Now().Add(1 * time.Hour), | ||
}, nil | ||
} | ||
auditedVendor := vendor.Auditor(successfulVendor) | ||
|
||
ctx, _ := audit.Context(context.Background()) | ||
claims := jwt.BuildkiteClaims{} | ||
repo := "example-repo" | ||
|
||
token, err := auditedVendor(ctx, claims, repo) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, token) | ||
assert.Equal(t, "https://example.com/repo", token.RepositoryURL) | ||
|
||
entry := audit.Log(ctx) | ||
assert.Empty(t, entry.Error) | ||
assert.Equal(t, []string{"https://example.com/repo"}, entry.Repositories) | ||
assert.Equal(t, []string{"contents:read"}, entry.Permissions) | ||
assert.NotZero(t, entry.ExpirySecs) | ||
} | ||
|
||
func TestAuditor_Mismatch(t *testing.T) { | ||
successfulVendor := func(ctx context.Context, claims jwt.BuildkiteClaims, repo string) (*vendor.PipelineRepositoryToken, error) { | ||
return nil, nil | ||
} | ||
auditedVendor := vendor.Auditor(successfulVendor) | ||
|
||
ctx, _ := audit.Context(context.Background()) | ||
claims := jwt.BuildkiteClaims{} | ||
repo := "example-repo" | ||
|
||
token, err := auditedVendor(ctx, claims, repo) | ||
|
||
assert.NoError(t, err) | ||
assert.Nil(t, token) | ||
|
||
entry := audit.Log(ctx) | ||
assert.Equal(t, "repository mismatch, no token vended", entry.Error) | ||
assert.Empty(t, entry.Repositories) | ||
assert.Empty(t, entry.Permissions) | ||
assert.Zero(t, entry.ExpirySecs) | ||
} | ||
|
||
func TestAuditor_Failure(t *testing.T) { | ||
failingVendor := func(ctx context.Context, claims jwt.BuildkiteClaims, repo string) (*vendor.PipelineRepositoryToken, error) { | ||
return nil, errors.New("vendor error") | ||
} | ||
auditedVendor := vendor.Auditor(failingVendor) | ||
|
||
ctx, _ := audit.Context(context.Background()) | ||
claims := jwt.BuildkiteClaims{} | ||
repo := "example-repo" | ||
|
||
token, err := auditedVendor(ctx, claims, repo) | ||
assert.Error(t, err) | ||
assert.Nil(t, token) | ||
|
||
entry := audit.Log(ctx) | ||
assert.Equal(t, "vendor failure: vendor error", entry.Error) | ||
assert.Empty(t, entry.Repositories) | ||
assert.Empty(t, entry.Permissions) | ||
assert.Zero(t, entry.ExpirySecs) | ||
} |
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