Skip to content

Commit 2d9923a

Browse files
authored
Merge pull request #9 from utilitywarehouse/admin-email
Expose the admin email as an environment variable, rather than hardcoding it
2 parents 5474a4e + d20d00a commit 2d9923a

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ Required environment variables:
1414
| SKM_CLIENT_ID | xxx.apps.googleusercontent.com | Google oidc client id |
1515
| SKM_CLIENT_SECRET | xxxxxxxx | Google oidc client secret |
1616
| SKM_CALLBACK_URL | https://app/callback | Callback URI where user will be redirected after successful Google interaction |
17-
| SKM_AWS_ACCESS_KEY_ID | AKIAXXXXXXXXXXXXXXXX | AWS access key |
18-
| SKM_AWS_SECRET_ACCESS_KEY | xxxxxxxxxxxxxxxxxxxxx | AWS secret access key |
1917
| SKM_AWS_BUCKET | bucket-name | AWS s3 bucket name |
2018
| SKM_SA_KEY_LOC | /etc/skm/sa-key.json | Location on disk where Google service account key is (json format) |
2119
| SKM_GROUPS | "group@gsuite-domain.com" | comma seperated list of groups that will be synced to s3 |
20+
| SKM_ADMIN_EMAIL | "admin-user@gsuite-domain.com" | A G-Suite admin user |
21+
22+
You will also need to configure the appropriate AWS credentials for your environment, as detailed [on this page](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials).
2223

2324
### client
2425

main.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/url"
1212
"os"
13+
"regexp"
1314
"strings"
1415
"sync"
1516

@@ -42,8 +43,7 @@ var (
4243
googleClientID = os.Getenv("SKM_CLIENT_ID")
4344
googleClientSecret = os.Getenv("SKM_CLIENT_SECRET")
4445
googleCallbackURL = os.Getenv("SKM_CALLBACK_URL")
45-
awsAccessKey = os.Getenv("SKM_AWS_ACCESS_KEY_ID")
46-
awsSecretKey = os.Getenv("SKM_AWS_SECRET_ACCESS_KEY")
46+
googleAdminEmail = os.Getenv("SKM_ADMIN_EMAIL")
4747
awsBucket = os.Getenv("SKM_AWS_BUCKET")
4848
saKeyLoc = os.Getenv("SKM_SA_KEY_LOC")
4949
groups = os.Getenv("SKM_GROUPS")
@@ -63,6 +63,47 @@ type tokenResponse struct {
6363
IDToken string `json:"id_token"`
6464
}
6565

66+
// Validate arguments
67+
func validate() {
68+
var err error
69+
70+
// Client ID
71+
clientIDRegex := regexp.MustCompile("^.*apps.googleusercontent.com$")
72+
if !clientIDRegex.MatchString(googleClientID) {
73+
log.Fatalln(googleClientID + " is not a valid client ID")
74+
}
75+
76+
// Client secret
77+
if googleClientSecret == "" {
78+
log.Fatalln("client secret must not be empty")
79+
}
80+
81+
// Callback URL
82+
u, err := url.ParseRequestURI(googleCallbackURL)
83+
if err != nil || (u.Host == "" || u.Scheme == "") {
84+
log.Fatalln(googleCallbackURL + " is not a valid URI")
85+
}
86+
87+
// Admin email string
88+
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
89+
if len(googleAdminEmail) > 254 || !emailRegex.MatchString(googleAdminEmail) {
90+
log.Fatalln(googleAdminEmail + " is not a valid email address")
91+
}
92+
93+
// AWS S3 bucket
94+
if awsBucket == "" {
95+
log.Fatalln("SKM_AWS_BUCKET must not be empty")
96+
}
97+
98+
// SA key location
99+
_, err = os.Stat(saKeyLoc)
100+
if os.IsNotExist(err) {
101+
log.Fatalln(saKeyLoc + " does not exist")
102+
} else if err != nil {
103+
log.Fatalln("can't stat " + saKeyLoc)
104+
}
105+
}
106+
66107
// Get the id_token and refresh_token from google
67108
func getTokens(clientID, clientSecret, code string) (*tokenResponse, error) {
68109
val := url.Values{}
@@ -125,7 +166,7 @@ func authenticatedClient() (client *http.Client) {
125166
log.Fatal(err)
126167
}
127168
conf, err := google.JWTConfigFromJSON(data, scopes...)
128-
conf.Subject = "mdonat@utilitywarehouse.co.uk"
169+
conf.Subject = googleAdminEmail
129170
if err != nil {
130171
log.Fatal(err)
131172
}
@@ -239,8 +280,14 @@ func authMapPage(am *authMap) http.Handler {
239280
}
240281

241282
func main() {
283+
validate()
284+
242285
adminClient := authenticatedClient()
243286
groups := strings.Split(groups, ",")
287+
if len(groups) == 0 {
288+
log.Fatalln("SKM_GROUPS can't be empty")
289+
}
290+
244291
am := &authMap{client: adminClient, inputGroups: groups}
245292
go am.sync()
246293

sync.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/aws/aws-sdk-go/aws"
14-
"github.com/aws/aws-sdk-go/aws/credentials"
1514
"github.com/aws/aws-sdk-go/aws/session"
1615
"github.com/aws/aws-sdk-go/service/s3/s3manager"
1716
)
@@ -123,10 +122,7 @@ func (am *authMap) groupsFromGoogle() ([]group, error) {
123122
func (am *authMap) postToAWS() {
124123
body, _ := json.Marshal(am)
125124

126-
sess, err := session.NewSession(&aws.Config{
127-
Region: aws.String("eu-west-1"),
128-
Credentials: credentials.NewStaticCredentials(awsAccessKey, awsSecretKey, ""),
129-
})
125+
sess, err := session.NewSession(&aws.Config{})
130126

131127
if err != nil {
132128
log.Printf("aws - Failed to create a session %v", err)

0 commit comments

Comments
 (0)