Skip to content

Commit

Permalink
Merge pull request #6 from shogo82148/implement-api-endpoint-validator
Browse files Browse the repository at this point in the history
implement API endpoint validator
  • Loading branch information
shogo82148 authored Sep 1, 2021
2 parents 2f377f0 + c54dade commit 93ec77d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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

0 comments on commit 93ec77d

Please sign in to comment.