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

apikey: Switch policy from allowedFields to query #3411

Merged
merged 3 commits into from
Nov 2, 2023
Merged
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
35 changes: 35 additions & 0 deletions apikey/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package apikey

import (
"context"

"github.com/99designs/gqlgen/graphql"
"github.com/target/goalert/permission"
"github.com/vektah/gqlparser/v2/gqlerror"
)

type Middleware struct{}

var _ graphql.OperationContextMutator = Middleware{}

func (Middleware) ExtensionName() string { return "GQLAPIKeyMiddleware" }
func (Middleware) Validate(schema graphql.ExecutableSchema) error { return nil }

func (Middleware) MutateOperationContext(ctx context.Context, rc *graphql.OperationContext) *gqlerror.Error {
p := PolicyFromContext(ctx)
if p == nil {
return nil
}

if p.Query != rc.RawQuery {
return &gqlerror.Error{
Err: permission.Unauthorized(),
Message: "wrong query for API key",
Extensions: map[string]interface{}{
"code": "invalid_query",
},
}
}

return nil
}
6 changes: 3 additions & 3 deletions apikey/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/target/goalert/permission"

// GQLPolicy is a GraphQL API key policy.
type GQLPolicy struct {
Version int
AllowedFields []string
Role permission.Role
Version int
Query string
Role permission.Role
}
65 changes: 28 additions & 37 deletions apikey/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"slices"
"sort"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -50,17 +48,17 @@ func NewStore(ctx context.Context, db *sql.DB, key keyring.Keyring) (*Store, err
}

type APIKeyInfo struct {
ID uuid.UUID
Name string
Description string
ExpiresAt time.Time
LastUsed *APIKeyUsage
CreatedAt time.Time
UpdatedAt time.Time
CreatedBy *uuid.UUID
UpdatedBy *uuid.UUID
AllowedFields []string
Role permission.Role
ID uuid.UUID
Name string
Description string
ExpiresAt time.Time
LastUsed *APIKeyUsage
CreatedAt time.Time
UpdatedAt time.Time
CreatedBy *uuid.UUID
UpdatedBy *uuid.UUID
Query string
Role permission.Role
}

func (s *Store) FindAllAdminGraphQLKeys(ctx context.Context) ([]APIKeyInfo, error) {
Expand Down Expand Up @@ -103,17 +101,17 @@ func (s *Store) FindAllAdminGraphQLKeys(ctx context.Context) ([]APIKeyInfo, erro
}

res = append(res, APIKeyInfo{
ID: k.ID,
Name: k.Name,
Description: k.Description,
ExpiresAt: k.ExpiresAt,
LastUsed: lastUsed,
CreatedAt: k.CreatedAt,
UpdatedAt: k.UpdatedAt,
CreatedBy: &k.CreatedBy.UUID,
UpdatedBy: &k.UpdatedBy.UUID,
AllowedFields: p.AllowedFields,
Role: p.Role,
ID: k.ID,
Name: k.Name,
Description: k.Description,
ExpiresAt: k.ExpiresAt,
LastUsed: lastUsed,
CreatedAt: k.CreatedAt,
UpdatedAt: k.UpdatedAt,
CreatedBy: &k.CreatedBy.UUID,
UpdatedBy: &k.UpdatedBy.UUID,
Query: p.Query,
Role: p.Role,
})
}

Expand Down Expand Up @@ -249,9 +247,9 @@ func (s *Store) AuthorizeGraphQL(ctx context.Context, tok, ua, ip string) (conte
type NewAdminGQLKeyOpts struct {
Name string
Desc string
Fields []string
Expires time.Time
Role permission.Role
Query string
}

// CreateAdminGraphQLKey will create a new GraphQL API key returning the ID and token.
Expand All @@ -261,31 +259,24 @@ func (s *Store) CreateAdminGraphQLKey(ctx context.Context, opt NewAdminGQLKeyOpt
return uuid.Nil, "", err
}

_, qErr := graphql2.QueryFields(opt.Query)
err = validate.Many(
qErr,
validate.IDName("Name", opt.Name),
validate.Text("Description", opt.Desc, 0, 255),
validate.Range("Fields", len(opt.Fields), 1, len(graphql2.SchemaFields())),
validate.OneOf("Role", opt.Role, permission.RoleAdmin, permission.RoleUser),
)
if time.Until(opt.Expires) <= 0 {
err = validate.Many(err, validation.NewFieldError("Expires", "must be in the future"))
}
for i, f := range opt.Fields {
if slices.Contains(graphql2.SchemaFields(), f) {
continue
}

err = validate.Many(err, validation.NewFieldError(fmt.Sprintf("Fields[%d]", i), "is not a valid field"))
}
if err != nil {
return uuid.Nil, "", err
}

sort.Strings(opt.Fields)
policyData, err := json.Marshal(GQLPolicy{
Version: 1,
AllowedFields: opt.Fields,
Role: opt.Role,
Version: 1,
Query: opt.Query,
Role: opt.Role,
})
if err != nil {
return uuid.Nil, "", err
Expand Down
Loading