-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grpc: add basic htpasswd authentication support
Add basic htpasswd auth to the gRPC endpoint, for feature parity with the HTTP endpoint. Until now, gRPC has been unauthenticated. To test, add the user/pass to bazel like so: --remote_cache=grpc://user:pass@address:port/
- Loading branch information
Showing
6 changed files
with
121 additions
and
8 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
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
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,101 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
grpc_status "google.golang.org/grpc/status" | ||
|
||
auth "github.com/abbot/go-http-auth" | ||
) | ||
|
||
var ( | ||
errNoMetadata = grpc_status.Error(codes.Unauthenticated, | ||
"no metadata found") | ||
errNoAuthMetadata = grpc_status.Error(codes.Unauthenticated, | ||
"no authentication metadata found") | ||
errAccessDenied = grpc_status.Error(codes.Unauthenticated, | ||
"access denied") | ||
) | ||
|
||
type GrpcBasicAuth struct { | ||
secrets auth.SecretProvider | ||
} | ||
|
||
func NewGrpcBasicAuth(secrets auth.SecretProvider) *GrpcBasicAuth { | ||
return &GrpcBasicAuth{secrets: secrets} | ||
} | ||
|
||
func (b *GrpcBasicAuth) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
username, password, err := getLogin(ss.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
if username == "" || password == "" { | ||
return errAccessDenied | ||
} | ||
|
||
if !b.allowed(username, password) { | ||
return errAccessDenied | ||
} | ||
|
||
return handler(srv, ss) | ||
} | ||
|
||
func (b *GrpcBasicAuth) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
username, password, err := getLogin(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if username == "" || password == "" { | ||
return nil, errAccessDenied | ||
} | ||
|
||
if !b.allowed(username, password) { | ||
return nil, errAccessDenied | ||
} | ||
|
||
return handler(ctx, req) | ||
} | ||
|
||
func getLogin(ctx context.Context) (username, password string, err error) { | ||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
return "", "", errNoMetadata | ||
} | ||
|
||
for k, v := range md { | ||
if k == ":authority" && len(v) > 0 { | ||
// When bazel is run with --remote_cache=grpc://user:pass@address/" | ||
// the value looks like "user:pass@address". | ||
fields := strings.SplitN(v[0], ":", 2) | ||
if len(fields) < 2 { | ||
continue | ||
} | ||
username = fields[0] | ||
|
||
fields = strings.SplitN(fields[1], "@", 2) | ||
if len(fields) < 2 { | ||
continue | ||
} | ||
password = fields[0] | ||
|
||
return username, password, nil | ||
} | ||
} | ||
|
||
return "", "", errNoAuthMetadata | ||
} | ||
|
||
func (b *GrpcBasicAuth) allowed(username, password string) bool { | ||
ignoredRealm := "" | ||
requiredSecret := b.secrets(username, ignoredRealm) | ||
if requiredSecret == "" { | ||
return false // User does not exist. | ||
} | ||
|
||
return auth.CheckSecret(password, requiredSecret) | ||
} |