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: Abort compiler when rpc fails as unauthenticated #2887

Merged
merged 3 commits into from
Oct 19, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: go install ./...

- name: test ./...
run: gotestsum --junitfile junit.xml -- --tags=examples ./...
run: gotestsum --junitfile junit.xml -- --tags=examples -timeout 20m ./...
env:
MYSQL_DATABASE: mysql
MYSQL_HOST: localhost
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/multierr"
"github.com/sqlc-dev/sqlc/internal/opts"
"github.com/sqlc-dev/sqlc/internal/rpc"
"github.com/sqlc-dev/sqlc/internal/source"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
Expand Down Expand Up @@ -88,6 +89,10 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
loc = e.Location
}
merr.Add(filename, src, loc, err)
// If this rpc unauthenticated error bubbles up, then all future parsing/analysis will fail
if errors.Is(err, rpc.ErrUnauthenticated) {
return nil, merr
}
continue
}
queryName := query.Metadata.Name
Expand Down
13 changes: 13 additions & 0 deletions internal/rpc/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package rpc

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const errMessageUnauthenticated = `rpc authentication failed

You may be using a sqlc auth token that was created for a different project,
or your auth token may have expired.`

var ErrUnauthenticated = status.New(codes.Unauthenticated, errMessageUnauthenticated).Err()
7 changes: 1 addition & 6 deletions internal/rpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ import (
"google.golang.org/grpc/status"
)

const errMessageUnauthenticated = `rpc authentication failed

You may be using a sqlc auth token that was created for a different project,
or your auth token may have expired.`

func UnaryInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
err := invoker(ctx, method, req, reply, cc, opts...)

switch status.Convert(err).Code() {
case codes.OK:
return nil
case codes.Unauthenticated:
return status.New(codes.Unauthenticated, errMessageUnauthenticated).Err()
return ErrUnauthenticated
default:
return err
}
Expand Down