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

implement API endpoint validator #6

Merged
merged 1 commit into from
Sep 1, 2021
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
4 changes: 4 additions & 0 deletions provider/github-app-token/github-app-token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type githubClient interface {
CreateStatus(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error)
ValidateAPIURL(url string) error
}

const (
Expand Down Expand Up @@ -88,6 +89,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) handle(ctx context.Context, req *requestBody) (*responseBody, error) {
if err := h.github.ValidateAPIURL(req.APIURL); err != nil {
return nil, err
}
if err := h.validateGitHubToken(ctx, req); err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion provider/github-app-token/github-app-token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
)

type githubClientMock struct {
CreateStatusFunc func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error)
CreateStatusFunc func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error)
ValidateAPIURLFunc func(url string) error
}

func (c *githubClientMock) CreateStatus(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) {
return c.CreateStatusFunc(ctx, token, owner, repo, ref, status)
}

func (c *githubClientMock) ValidateAPIURL(url string) error {
return c.ValidateAPIURLFunc(url)
}

func TestValidateGitHubToken(t *testing.T) {
h := &Handler{
github: &githubClientMock{
Expand Down Expand Up @@ -47,6 +52,9 @@ func TestValidateGitHubToken(t *testing.T) {
},
}, nil
},
ValidateAPIURLFunc: func(url string) error {
return nil
},
},
}
err := h.validateGitHubToken(context.Background(), &requestBody{
Expand Down Expand Up @@ -96,6 +104,9 @@ func TestValidateGitHubToken_InvalidCreator(t *testing.T) {
},
}, nil
},
ValidateAPIURLFunc: func(url string) error {
return nil
},
},
}
err := h.validateGitHubToken(context.Background(), &requestBody{
Expand Down
21 changes: 20 additions & 1 deletion provider/github-app-token/github/github.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -10,7 +11,7 @@ import (
)

const (
githubUserAgent = "actions-aws-assume-role/1.0"
githubUserAgent = "actions-github-token/1.0"
defaultAPIBaseURL = "https://api.github.com"
)

Expand Down Expand Up @@ -45,6 +46,24 @@ func NewClient(httpClient *http.Client) *Client {
}
}

func (c *Client) ValidateAPIURL(url string) error {
u, err := canonicalURL(url)
if err != nil {
return err
}
if u != c.baseURL {
if c.baseURL == defaultAPIBaseURL {
return errors.New(
"it looks that you use GitHub Enterprise Server, " +
"but the credential provider doesn't support it. " +
"I recommend you to build your own credential provider",
)
}
return errors.New("your api server is not verified by the credential provider")
}
return nil
}

type UnexpectedStatusCodeError struct {
StatusCode int
}
Expand Down