-
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.
- Loading branch information
Showing
5 changed files
with
203 additions
and
54 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
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,54 @@ | ||
package jwtauth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/sdk/helper/logging" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
func createTestBackend(t *testing.T) (*multiroleJWTAuthBackend, logical.Storage) { | ||
config := &logical.BackendConfig{ | ||
Logger: logging.NewVaultLogger(log.Trace), | ||
|
||
System: &logical.StaticSystemView{ | ||
DefaultLeaseTTLVal: time.Hour * 12, | ||
MaxLeaseTTLVal: time.Hour * 24, | ||
}, | ||
StorageView: &logical.InmemStorage{}, | ||
} | ||
|
||
logicalBackend, err := Factory(context.Background(), config) | ||
if err != nil { | ||
t.Fatalf("unable to create backend: %v", err) | ||
} | ||
|
||
backend, ok := logicalBackend.(*multiroleJWTAuthBackend) | ||
if !ok { | ||
t.Fatal("backend is not a multiroleJWTAuthBackend") | ||
} | ||
|
||
return backend, config.StorageView | ||
} | ||
|
||
func testConfig() map[string]any { | ||
return map[string]any{ | ||
"roles": map[string]any{ | ||
"foo": map[string]any{ | ||
"project_path": []any{"foo", "bar"}, | ||
}, | ||
"bar": map[string]any{ | ||
"namespace_path": []any{"c"}, | ||
"ref": []any{"master", "main"}, | ||
}, | ||
"baz": map[string]any{ | ||
"project_path": []any{"baz"}, | ||
}, | ||
}, | ||
"jwt_auth_host": "http://localhost:8200", | ||
"jwt_auth_path": "foo/jwt", | ||
} | ||
} |
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
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,112 @@ | ||
package jwtauth | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"testing" | ||
|
||
jwt "github.com/golang-jwt/jwt/v5" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/hashicorp/vault-client-go/schema" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
func TestLogin_Write(t *testing.T) { | ||
t.Parallel() | ||
backend, storage := createTestBackend(t) | ||
|
||
configData := map[string]any{ | ||
"roles": map[string]any{ | ||
"foo": map[string]any{ | ||
"project_path": []any{"foo"}, | ||
}, | ||
"foobar": map[string]any{ | ||
"namespace_path": []any{"ns"}, | ||
"project_path": []any{"foo", "bar"}, | ||
}, | ||
"baz": map[string]any{ | ||
"project_path": []any{"baz"}, | ||
}, | ||
}, | ||
"jwt_auth_host": "http://localhost:8200", | ||
"jwt_auth_path": "jwt", | ||
} | ||
req := &logical.Request{ | ||
Operation: logical.UpdateOperation, | ||
Path: "config", | ||
Storage: storage, | ||
Data: configData, | ||
} | ||
|
||
resp, err := backend.HandleRequest(context.Background(), req) | ||
if err != nil || (resp != nil && resp.IsError()) { | ||
t.Fatalf("err:%s resp:%#v\n", err, resp) | ||
} | ||
|
||
var policyClient fakePolicyFetcher = func(_ context.Context, request schema.JwtLoginRequest) ([]string, error) { | ||
return []string{request.Role + "-policy"}, nil | ||
} | ||
backend.policyClient = policyClient | ||
|
||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
t.Fatalf("unable to create private key: %s", err.Error()) | ||
} | ||
|
||
for i, tt := range []struct { | ||
claims jwt.MapClaims | ||
policies []string | ||
}{ | ||
{ | ||
claims: jwt.MapClaims{ | ||
"project_path": "foo", | ||
}, | ||
policies: []string{"foo-policy"}, | ||
}, | ||
{ | ||
claims: jwt.MapClaims{ | ||
"project_path": "foo", | ||
"namespace_path": "ns", | ||
}, | ||
policies: []string{"foo-policy", "foobar-policy"}, | ||
}, | ||
{ | ||
claims: jwt.MapClaims{ | ||
"project_path": "baz", | ||
}, | ||
policies: []string{"baz-policy"}, | ||
}, | ||
} { | ||
token, err := jwt.NewWithClaims(jwt.SigningMethodPS512, tt.claims).SignedString(privateKey) | ||
if err != nil { | ||
t.Fatalf("unable to sign jwt: %s", err.Error()) | ||
} | ||
|
||
req = &logical.Request{ | ||
Operation: logical.UpdateOperation, | ||
Path: "login", | ||
Storage: storage, | ||
Data: map[string]any{ | ||
"jwt": token, | ||
}, | ||
} | ||
|
||
resp, err = backend.HandleRequest(context.Background(), req) | ||
if err != nil || (resp != nil && resp.IsError()) { | ||
t.Fatalf("err:%s resp:%#v\n", err, resp) | ||
} | ||
|
||
opt := cmpopts.SortSlices(func(a, b string) bool { return a < b }) | ||
if diff := cmp.Diff(resp.Auth.Policies, tt.policies, opt); diff != "" { | ||
t.Fatalf("Test case %v failed with diff:\n%s", i, diff) | ||
} | ||
} | ||
} | ||
|
||
type fakePolicyFetcher func(context.Context, schema.JwtLoginRequest) ([]string, error) | ||
|
||
func (c fakePolicyFetcher) policies(ctx context.Context, request schema.JwtLoginRequest) ([]string, error) { | ||
return c(ctx, request) | ||
} |