forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from okteto/jpf-okteto/plat-351
Initialize v0.12.5-okteto branch (PLAT-351)
- Loading branch information
Showing
117 changed files
with
19,937 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.git | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# vendor folders | ||
vendor | ||
|
||
# Test binary, built with go test -c | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# dlv binary | ||
__debug_bin |
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,79 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
var ( | ||
errMissingMetadata = status.Errorf(codes.InvalidArgument, "missing metadata") | ||
errInvalidToken = status.Errorf(codes.Unauthenticated, "invalid okteto token, run `okteto login` and try again") | ||
) | ||
|
||
// Service is the service buildkit can use to authenticate | ||
type Service struct { | ||
endpoint string | ||
client *http.Client | ||
} | ||
|
||
// NewService returns an AuthService configured to use endpoint | ||
func NewService(endpoint string) *Service { | ||
return &Service{ | ||
endpoint: endpoint, | ||
client: &http.Client{}, | ||
} | ||
} | ||
|
||
// EnsureValidToken validates that the context includes authentication metadata | ||
// and that it's valild | ||
func (s *Service) EnsureValidToken(ctx context.Context) (context.Context, error) { | ||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
return ctx, errMissingMetadata | ||
} | ||
|
||
if !s.valid(md["authorization"]) { | ||
return ctx, errInvalidToken | ||
} | ||
|
||
return ctx, nil | ||
} | ||
|
||
func (s *Service) valid(authorization []string) bool { | ||
if len(authorization) < 1 { | ||
logrus.Error("request didn't contain an authorization header") | ||
return false | ||
} | ||
|
||
req, err := http.NewRequest("POST", s.endpoint, nil) | ||
if err != nil { | ||
logrus.Errorf("couldn't create request: %s", err) | ||
return false | ||
} | ||
|
||
// TODO: the OKTETO CLI should not be sending a bearer token, but just the plain token | ||
a := strings.TrimPrefix(authorization[0], "Bearer") | ||
a = "Bearer " + a | ||
|
||
req.Header.Add("Authorization", a) | ||
resp, err := s.client.Do(req) | ||
if err != nil { | ||
logrus.Errorf("authentication request failed: %s", err) | ||
return false | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
logrus.Errorf("authentication request failed due to a bad token: %d", resp.StatusCode) | ||
return false | ||
} | ||
|
||
return true | ||
} |
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
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
Oops, something went wrong.