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

feat: supporting namespaced auth tokens #2352

Merged
merged 81 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
975b177
feat(wip): beginnings of supporting namespaced auth tokens
markphelps Nov 7, 2023
e8c18e0
feat(wip): start work on ui for adding namespaced token
markphelps Nov 8, 2023
6787632
chore: merge other branch
markphelps Nov 8, 2023
ec775f4
chore: enable interceptor
markphelps Nov 8, 2023
03f0f9e
chore: set namespace key on token form submit
markphelps Nov 8, 2023
9be5e05
chore: snakecase namespace key
markphelps Nov 8, 2023
9a294b3
chore: add IT for namespaced token creation
markphelps Nov 9, 2023
cb0f0b0
chore: add ui IT for namespaced token
markphelps Nov 9, 2023
2fdfa28
chore: add todo
markphelps Nov 9, 2023
ebedcc2
chore: add scoped method for namespace requests
markphelps Nov 9, 2023
9090911
chore: fix it
markphelps Nov 9, 2023
3f01fe9
chore: fix test
markphelps Nov 9, 2023
53b6cce
chore: try to fix ui test
markphelps Nov 9, 2023
51f9150
Merge branch 'main' into namespace-auth-token
markphelps Nov 9, 2023
0752892
chore: comment out auth for local config
markphelps Nov 9, 2023
3535974
Merge branch 'namespace-auth-token' of https://github.com/flipt-io/fl…
markphelps Nov 9, 2023
fce1e3f
chore: fix token
markphelps Nov 9, 2023
1622649
chore: token table
markphelps Nov 9, 2023
0be885c
chore: add more tests
markphelps Nov 10, 2023
8ef5702
Merge branch 'main' into namespace-auth-token
markphelps Nov 10, 2023
542edcf
chore: make interceptor panic if no auth
markphelps Nov 10, 2023
f7948cd
chore: try to add / fix ITs
markphelps Nov 10, 2023
49ebe2d
chore: try to fix api test
markphelps Nov 10, 2023
42d70d3
chore: clean up config/local.yml
markphelps Nov 10, 2023
96ffb54
chore: fix creating namespaced token in IT
markphelps Nov 10, 2023
f05a43a
chore: skip interceptors for non-auth required servers
markphelps Nov 10, 2023
9ab4640
chore: more IT woes
markphelps Nov 10, 2023
bd50f81
chore: omg ITs
markphelps Nov 10, 2023
82c9927
chore: gah!
markphelps Nov 10, 2023
0335184
fix(auth): support batch requests with namespaced tokens
GeorgeMac Nov 11, 2023
0181d7f
test(auth): ensure namespaced token cant access meta
GeorgeMac Nov 11, 2023
f64115d
Merge branch 'main' into namespace-auth-token
markphelps Nov 11, 2023
4f963a5
chore: move auth middleware to subpackages
markphelps Nov 11, 2023
bec2d5e
chore: refactor to opt in to namespace auth
markphelps Nov 11, 2023
994f358
chore: wire through auth opts
markphelps Nov 11, 2023
b9ed45f
chore: rename methods/interface
markphelps Nov 11, 2023
e7b634e
chore: fix test
markphelps Nov 11, 2023
a1639c2
chore: try to fix ITs
markphelps Nov 11, 2023
57691b9
chore: hopefully fix scoped servers
markphelps Nov 12, 2023
3af7d0b
Merge branch 'main' into namespace-auth-token
markphelps Nov 12, 2023
51bf24a
fix: db root on mac after refactor
markphelps Nov 12, 2023
6f0c8b2
Merge branch 'fix-db-root-mac' into namespace-auth-token
markphelps Nov 12, 2023
013c898
chore: typo
markphelps Nov 12, 2023
791e843
chore: try to fix docker issue on mac
markphelps Nov 12, 2023
6f1ee97
chore: fix middleware
markphelps Nov 12, 2023
ea0dd21
chore: fix default db path
markphelps Nov 12, 2023
6615fef
chore: run short tests for macos
markphelps Nov 12, 2023
e4106cf
Merge branch 'fix-db-root-mac' into namespace-auth-token
markphelps Nov 12, 2023
f222c66
chore: use go test if no gotest
markphelps Nov 12, 2023
63a6f31
Merge branch 'fix-db-root-mac' into namespace-auth-token
markphelps Nov 12, 2023
82d99ca
chore: wat are you talking about computers!?
markphelps Nov 12, 2023
e8a9066
chore: wat are you talking about computers!?
markphelps Nov 12, 2023
5e9fe5b
chore: fix mage test
markphelps Nov 12, 2023
a8d0bd6
chore: fix validation test
markphelps Nov 12, 2023
e903fc9
Merge branch 'fix-db-root-mac' into namespace-auth-token
markphelps Nov 12, 2023
3ecef2b
chore: namespaced token shouldnt be able to do anything with token en…
markphelps Nov 12, 2023
33a13a6
Update server.go
markphelps Nov 13, 2023
2893149
chore: merge origin/main
markphelps Nov 13, 2023
73e0481
chore: fix typo
markphelps Nov 13, 2023
f45b8d3
Merge branch 'main' into namespace-auth-token
markphelps Nov 13, 2023
c384ac3
Merge branch 'main' into namespace-auth-token
markphelps Nov 13, 2023
6d70364
chore: comments for auth test
markphelps Nov 13, 2023
2ee8899
Merge branch 'main' into namespace-auth-token
markphelps Nov 13, 2023
dc7cb7d
Merge branch 'namespace-auth-token' of https://github.com/flipt-io/fl…
markphelps Nov 13, 2023
70c1759
chore: try to fix auth middleware
markphelps Nov 13, 2023
54f793a
chore: tests
markphelps Nov 13, 2023
a2b92bd
Revert "chore: tests"
markphelps Nov 13, 2023
c7738a6
Revert "chore: try to fix auth middleware"
markphelps Nov 13, 2023
c059c90
chore: disallow namespace requests for auth server
markphelps Nov 13, 2023
2b08201
chore: i am at my wits end
markphelps Nov 13, 2023
58e8337
Merge branch 'main' into namespace-auth-token
markphelps Nov 13, 2023
5b7fe28
fix(tests/integration): ensure default namespace token appropriately
GeorgeMac Nov 14, 2023
fd186a4
Merge branch 'main' into namespace-auth-token
GeorgeMac Nov 14, 2023
4c112e9
fix(tests/integration): only create namespace in api tests
GeorgeMac Nov 14, 2023
c4cfcee
Merge branch 'namespace-auth-token' of github.com:flipt-io/flipt into…
GeorgeMac Nov 14, 2023
2eb8385
fix(auth): support create namespace with namespaced scoped token
GeorgeMac Nov 14, 2023
96f05e1
chore: add some minor unit test coverage
markphelps Nov 14, 2023
fe94c73
test(auth/middleware): ensure namespaced batch cases behave expectedly
GeorgeMac Nov 14, 2023
722d3be
Merge branch 'namespace-auth-token' of github.com:flipt-io/flipt into…
GeorgeMac Nov 14, 2023
c139555
chore: more tests
markphelps Nov 14, 2023
d70f79b
Merge branch 'namespace-auth-token' of https://github.com/flipt-io/fl…
markphelps Nov 14, 2023
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
29 changes: 22 additions & 7 deletions build/testing/integration/api/authenticated.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,30 @@ func Authenticated(t *testing.T, client sdk.SDK) {
t.Run("Static Token", func(t *testing.T) {
t.Log(`Create token.`)

resp, err := client.Auth().AuthenticationMethodTokenService().CreateToken(ctx, &auth.CreateTokenRequest{
Name: "Access Token",
Description: "Some kind of access token.",
t.Run("With name and description", func(t *testing.T) {
resp, err := client.Auth().AuthenticationMethodTokenService().CreateToken(ctx, &auth.CreateTokenRequest{
Name: "Access Token",
Description: "Some kind of access token.",
})
require.NoError(t, err)

assert.NotEmpty(t, resp.ClientToken)
assert.Equal(t, "Access Token", resp.Authentication.Metadata["io.flipt.auth.token.name"])
assert.Equal(t, "Some kind of access token.", resp.Authentication.Metadata["io.flipt.auth.token.description"])
})
require.NoError(t, err)

assert.NotEmpty(t, resp.ClientToken)
assert.Equal(t, "Access Token", resp.Authentication.Metadata["io.flipt.auth.token.name"])
assert.Equal(t, "Some kind of access token.", resp.Authentication.Metadata["io.flipt.auth.token.description"])
t.Run("With name and namespaceKey", func(t *testing.T) {
resp, err := client.Auth().AuthenticationMethodTokenService().CreateToken(ctx, &auth.CreateTokenRequest{
Name: "Scoped Access Token",
NamespaceKey: "some-namespace",
})
require.NoError(t, err)

assert.NotEmpty(t, resp.ClientToken)
assert.Equal(t, "Scoped Access Token", resp.Authentication.Metadata["io.flipt.auth.token.name"])
assert.Empty(t, resp.Authentication.Metadata["io.flipt.auth.token.description"])
assert.Equal(t, "some-namespace", resp.Authentication.Metadata["io.flipt.auth.token.namespace"])
})
})

t.Run("Expire Self", func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions config/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ log:
# ui:
# enabled: true

# authentication:
# required: false
# session:
# domain: "localhost:8080"
# secure: false
# methods:
# token:
# enabled: true
# bootstrap:
# token: "secret"
# expiration: 24h
# cleanup:
# interval: 2h
# grace_period: 48h

cors:
enabled: true
allowed_origins: ["*"]
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func authenticationGRPC(
interceptors = append(interceptors, auth.EmailMatchingInterceptor(logger, rgxs))
}

if authCfg.Methods.Token.Enabled {
interceptors = append(interceptors, auth.NamespaceMatchingInterceptor(logger))
}

logger.Info("authentication middleware enabled")
}

Expand Down
19 changes: 14 additions & 5 deletions internal/server/auth/method/token/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
storageMetadataNameKey = "io.flipt.auth.token.name"
storageMetadataDescriptionKey = "io.flipt.auth.token.description"
storageMetadataNamespaceKey = "io.flipt.auth.token.namespace"
)

// Server is an implementation of auth.AuthenticationMethodTokenServiceServer
Expand Down Expand Up @@ -40,17 +41,25 @@ func (s *Server) RegisterGRPC(server *grpc.Server) {
// CreateToken adapts and delegates the token request to the backing AuthenticationStore.
//
// Implicitly, the Authentication created will be of type auth.Method_TOKEN.
// Name and Description are both stored in Authentication.Metadata.
// Name, Description, and NamespaceKey are both stored in Authentication.Metadata.
// Given the token is created successfully, the generate clientToken string is returned.
// Along with the created Authentication, which includes it's identifier and associated timestamps.
func (s *Server) CreateToken(ctx context.Context, req *auth.CreateTokenRequest) (*auth.CreateTokenResponse, error) {
metadata := map[string]string{
storageMetadataNameKey: req.GetName(),
}

if req.GetDescription() != "" {
metadata[storageMetadataDescriptionKey] = req.GetDescription()
}
if req.GetNamespaceKey() != "" {
metadata[storageMetadataNamespaceKey] = req.GetNamespaceKey()
}

clientToken, authentication, err := s.store.CreateAuthentication(ctx, &storageauth.CreateAuthenticationRequest{
Method: auth.Method_METHOD_TOKEN,
ExpiresAt: req.ExpiresAt,
Metadata: map[string]string{
storageMetadataNameKey: req.GetName(),
storageMetadataDescriptionKey: req.GetDescription(),
},
Metadata: metadata,
})
if err != nil {
return nil, fmt.Errorf("attempting to create token: %w", err)
Expand Down
10 changes: 6 additions & 4 deletions internal/server/auth/method/token/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
middleware "go.flipt.io/flipt/internal/server/middleware/grpc"
Expand All @@ -27,7 +26,7 @@ func TestServer(t *testing.T) {
store = memory.NewStore()
listener = bufconn.Listen(1024 * 1024)
server = grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
middleware.ErrorUnaryInterceptor,
),
)
Expand Down Expand Up @@ -65,15 +64,17 @@ func TestServer(t *testing.T) {

// attempt to create token
resp, err := client.CreateToken(ctx, &auth.CreateTokenRequest{
Name: "access_all_areas",
Description: "Super secret skeleton key",
Name: "access_all_areas",
Description: "Super secret skeleton key",
NamespaceKey: "my-namespace",
})
require.NoError(t, err)

// assert auth is as expected
metadata := resp.Authentication.Metadata
assert.Equal(t, "access_all_areas", metadata["io.flipt.auth.token.name"])
assert.Equal(t, "Super secret skeleton key", metadata["io.flipt.auth.token.description"])
assert.Equal(t, "my-namespace", metadata["io.flipt.auth.token.namespace"])

// ensure client token can be used on store to fetch authentication
// and that the authentication returned matches the one received
Expand All @@ -94,5 +95,6 @@ func TestServer(t *testing.T) {
// invalid expires at, nanos must be positive
ExpiresAt: &timestamppb.Timestamp{Nanos: -1},
})

require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "attempting to create token: invalid expiry time: nanos:-1"))
}
47 changes: 47 additions & 0 deletions internal/server/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"go.flipt.io/flipt/internal/containers"
"go.flipt.io/flipt/rpc/flipt"
authrpc "go.flipt.io/flipt/rpc/flipt/auth"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand Down Expand Up @@ -176,6 +177,52 @@ func EmailMatchingInterceptor(logger *zap.Logger, rgxs []*regexp.Regexp) grpc.Un
}
}

func NamespaceMatchingInterceptor(logger *zap.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
auth := GetAuthenticationFrom(ctx)

if auth == nil {
return handler(ctx, req)
}

// this mechanism only applies to static toke authentications
if auth.Method != authrpc.Method_METHOD_TOKEN {
return handler(ctx, req)
}
Comment on lines +216 to +219
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking out loud (this is probably is good for this pass):

In the future it would be nice to be able to get an associated policy from the authentication.
Instead of the enforcing middleware having to know about the types of authentications, it should just ask for the policy / authorization. Then inspect that common policy structure / interface as to whether or not the request is valid.

That might be a bit over abstracted for this first pass, but will make extending policy to other authentication types easier.


namespace, ok := auth.Metadata["io.flipt.auth.token.namespace"]
if !ok {
// if no namespace is provided then we should allow the request
return handler(ctx, req)
}

namespace = strings.TrimSpace(namespace)

if namespace == "" {
return handler(ctx, req)
}

nsReq, ok := req.(flipt.Namespaced)
if !ok {
// if the the token has a namespace but the request does not then we should reject the request
logger.Error("unauthenticated", zap.String("reason", "namespace is not allowed"))
return ctx, errUnauthenticated
}

reqNamespace := nsReq.GetNamespaceKey()
if reqNamespace == "" {
reqNamespace = "default"
}

if reqNamespace != namespace {
logger.Error("unauthenticated", zap.String("reason", "namespace is not allowed"))
return ctx, errUnauthenticated
}

return handler(ctx, req)
}
}

func clientTokenFromMetadata(md metadata.MD) (string, error) {
if authenticationHeader := md.Get(authenticationHeaderKey); len(authenticationHeader) > 0 {
return clientTokenFromAuthorization(authenticationHeader[0])
Expand Down
Loading