Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: Abort compiler when rpc fails as unauthenticated
  • Loading branch information
andrewmbenton committed Oct 19, 2023
commit 86dddf2dd0b1e0a10909a96d167a93749aa52856
6 changes: 6 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 @@ -64,6 +65,7 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
if err != nil {
return nil, err
}
fileLoop:
for _, filename := range files {
blob, err := os.ReadFile(filename)
if err != nil {
Expand All @@ -88,6 +90,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) {
break fileLoop
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can just return early here 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