Skip to content

Commit 86d8180

Browse files
authored
Merge pull request netlify#3 from netlify/mask-access-tokens
Mask access tokens
2 parents 7a624ca + 2b25348 commit 86d8180

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

api/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"net/url"
88
"regexp"
99
"strings"
10+
11+
"github.com/netlify/git-gateway/conf"
1012
)
1113

1214
// GitHubGateway acts as a proxy to GitHub
1315
type GitHubGateway struct {
1416
proxy *httputil.ReverseProxy
1517
}
1618

17-
const defaultEndpoint = "https://api.github.com"
18-
1919
var pathRegexp = regexp.MustCompile("^/github/?")
2020
var allowedRegexp = regexp.MustCompile("^/github/(git|contents|pulls|branches)/")
2121

@@ -69,7 +69,7 @@ func (gh *GitHubGateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6969
if config.GitHub.Endpoint != "" {
7070
endpoint = config.GitHub.Endpoint
7171
} else {
72-
endpoint = defaultEndpoint
72+
endpoint = conf.DefaultGitHubEndpoint
7373
}
7474
var apiURL string
7575
if strings.HasSuffix(endpoint, "/") {

conf/configuration.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package conf
22

33
import (
4+
"encoding/json"
45
"os"
6+
"strings"
57

68
"github.com/joho/godotenv"
79
"github.com/kelseyhightower/envconfig"
810
"github.com/netlify/netlify-commons/nconf"
911
)
1012

13+
const DefaultGitHubEndpoint = "https://api.github.com"
14+
1115
type GitHubConfig struct {
1216
AccessToken string `envconfig:"ACCESS_TOKEN" json:"access_token"`
1317
Endpoint string `envconfig:"ENDPOINT" json:"endpoint"`
@@ -48,6 +52,29 @@ type Configuration struct {
4852
Roles []string `envconfig:"ROLES" json:"roles"`
4953
}
5054

55+
func maskAccessToken(token string) string {
56+
return strings.Repeat("*", len(token))
57+
}
58+
59+
func githubEndpoint(endpoint string) string {
60+
if endpoint == "" {
61+
return DefaultGitHubEndpoint
62+
}
63+
return endpoint
64+
}
65+
66+
func (gh *GitHubConfig) MarshalJSON() ([]byte, error) {
67+
return json.Marshal(&struct {
68+
AccessToken string `json:"access_token"`
69+
Endpoint string `json:"endpoint"`
70+
Repo string `json:"repo"`
71+
}{
72+
AccessToken: maskAccessToken(gh.AccessToken),
73+
Endpoint: githubEndpoint(gh.Endpoint),
74+
Repo: gh.Repo,
75+
})
76+
}
77+
5178
func loadEnvironment(filename string) error {
5279
var err error
5380
if filename != "" {

0 commit comments

Comments
 (0)