Skip to content

Commit cc876e1

Browse files
committed
Target default endpoint for Gen3 Workflow (localhost:8080/s3)
1 parent 8f0740b commit cc876e1

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

authorizer/authorizer.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package main
22

33
import (
4-
"bufio"
54
"context"
5+
"encoding/json"
66
"fmt"
77
"net/http"
8-
"os"
98
"strings"
109

1110
"example.com/auth"
@@ -22,7 +21,7 @@ func (ExampleAuthorizer) Hooks() []string {
2221
return []string{"contents"}
2322
}
2423

25-
func (ExampleAuthorizer) Authorize(authHeader http.Header, task tes.TesTask) (auth.Auth, error) {
24+
func (ExampleAuthorizer) Authorize(authHeader http.Header, task tes.Task) (auth.Auth, error) {
2625
// TOOD: Currently we're just using the first Authorization header in the request
2726
// How might we support multiple Authorization headers?
2827
if authHeader == nil {
@@ -52,41 +51,24 @@ func (ExampleAuthorizer) Authorize(authHeader http.Header, task tes.TesTask) (au
5251
}
5352

5453
func (ExampleAuthorizer) getUser(user string) (auth.Auth, error) {
55-
// Check if the user is authorized
56-
// Read the "internal" User Database
57-
// Here we're just using a CSV file to represent the list of authorized users
58-
// A real-world example would use a database or an external service (e.g. OAuth)
59-
userFile := os.Getenv("EXAMPLE_USERS")
60-
if userFile == "" {
61-
log.Info(context.Background(), "EXAMPLE_USERS not set, using default example-users.csv")
62-
userFile = "authorizer/example-users.csv"
63-
}
64-
65-
file, err := os.Open(userFile)
54+
// Query the external service for user authorization
55+
url := fmt.Sprintf("http://localhost:8080/s3?user=%s", user)
56+
resp, err := http.Get(url)
6657
if err != nil {
6758
return auth.Auth{}, err
6859
}
69-
defer file.Close()
70-
71-
scanner := bufio.NewScanner(file)
72-
lineNumber := 0
73-
for scanner.Scan() {
74-
lineNumber++
75-
line := scanner.Text()
76-
77-
if strings.Contains(line, user) {
78-
result := strings.Split(line, ",")
60+
defer resp.Body.Close()
7961

80-
auth := auth.Auth{
81-
User: result[0],
82-
Token: result[1],
83-
}
62+
if resp.StatusCode != http.StatusOK {
63+
return auth.Auth{}, fmt.Errorf("User %s not found", user)
64+
}
8465

85-
return auth, nil
86-
}
66+
var creds auth.Auth
67+
if err := json.NewDecoder(resp.Body).Decode(&creds); err != nil {
68+
return auth.Auth{}, err
8769
}
8870

89-
return auth.Auth{}, fmt.Errorf("User %s not found", user)
71+
return creds, nil
9072
}
9173

9274
func main() {

authorizer/authorizer_test.go

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"net/http"
67
"os"
@@ -10,16 +11,16 @@ import (
1011
"example.com/tes"
1112
)
1213

13-
func readExampleTask(filename string) tes.TesTask {
14+
func readExampleTask(filename string) tes.Task {
1415
data, err := os.ReadFile(filename)
1516
if err != nil {
1617
panic(err)
1718
}
1819

19-
var task tes.TesTask
20+
var task tes.Task
2021
err = json.Unmarshal(data, &task)
2122
if err != nil {
22-
return tes.TesTask{}
23+
return tes.Task{}
2324
}
2425

2526
return task
@@ -31,28 +32,35 @@ func TestValidUser(t *testing.T) {
3132

3233
// Read example task
3334
task := readExampleTask("../example-tasks/hello-world.json")
35+
taskData, _ := json.Marshal(task)
3436

3537
// Create an Authorization Header to pass to the authorizer
36-
authHeader := http.Header{
37-
"Authorization": []string{"Bearer Alyssa P. Hacker"},
38-
}
39-
40-
authenticator := ExampleAuthorizer{}
41-
resp, err := authenticator.Authorize(authHeader, task)
38+
req, _ := http.NewRequest("POST", "http://localhost:8080/s3", bytes.NewBuffer(taskData))
39+
req.Header.Set("Authorization", "Bearer Alyssa P. Hacker")
40+
client := &http.Client{}
41+
resp, err := client.Do(req)
4242

4343
if err != nil {
4444
t.Fatalf("Expected no error, got %v", err)
4545
}
46+
defer resp.Body.Close()
47+
48+
if resp.StatusCode != http.StatusOK {
49+
t.Errorf("Expected status code 200, got %v", resp.StatusCode)
50+
}
51+
52+
var authResp auth.Auth
53+
json.NewDecoder(resp.Body).Decode(&authResp)
4654

4755
expected := auth.Auth{
4856
User: "Alyssa P. Hacker",
4957
Token: "<Alyssa's Secret>",
5058
}
5159

52-
if resp.User != expected.User || resp.Token != expected.Token {
60+
if authResp.User != expected.User || authResp.Token != expected.Token {
5361
t.Errorf("Expected (%s, %s), got (%s, %s)",
5462
expected.User, expected.Token,
55-
resp.User, resp.Token)
63+
authResp.User, authResp.Token)
5664
}
5765
}
5866

@@ -62,17 +70,21 @@ func TestInvalidUser(t *testing.T) {
6270

6371
// Read example task
6472
task := readExampleTask("../example-tasks/hello-world.json")
73+
taskData, _ := json.Marshal(task)
6574

6675
// Create an Authorization Header to pass to the authorizer
67-
authHeader := http.Header{
68-
"Authorization": []string{"Bearer Foo"},
69-
}
76+
req, _ := http.NewRequest("POST", "http://localhost:8080/s3", bytes.NewBuffer(taskData))
77+
req.Header.Set("Authorization", "Bearer Foo")
78+
client := &http.Client{}
79+
resp, err := client.Do(req)
7080

71-
authenticator := ExampleAuthorizer{}
72-
_, err := authenticator.Authorize(authHeader, task)
81+
if err != nil {
82+
t.Fatalf("Expected no error, got %v", err)
83+
}
84+
defer resp.Body.Close()
7385

74-
if err == nil {
75-
t.Fatalf("Expected 401 Unauthorized error, got %v", err)
86+
if resp.StatusCode != http.StatusUnauthorized {
87+
t.Errorf("Expected status code 401, got %v", resp.StatusCode)
7688
}
7789
}
7890

@@ -82,17 +94,21 @@ func TestInvalidAuthHeader(t *testing.T) {
8294

8395
// Read example task
8496
task := readExampleTask("../example-tasks/hello-world.json")
97+
taskData, _ := json.Marshal(task)
8598

8699
// Create an Authorization Header to pass to the authorizer
87-
authHeader := http.Header{
88-
"Authorization": []string{"Basic Alyssa P. Hacker"},
89-
}
100+
req, _ := http.NewRequest("POST", "http://localhost:8080/s3", bytes.NewBuffer(taskData))
101+
req.Header.Set("Authorization", "Basic Alyssa P. Hacker")
102+
client := &http.Client{}
103+
resp, err := client.Do(req)
90104

91-
authenticator := ExampleAuthorizer{}
92-
_, err := authenticator.Authorize(authHeader, task)
105+
if err != nil {
106+
t.Fatalf("Expected no error, got %v", err)
107+
}
108+
defer resp.Body.Close()
93109

94-
if err == nil {
95-
t.Fatalf("Expected 401 Unauthorized error, got %v", err)
110+
if resp.StatusCode != http.StatusUnauthorized {
111+
t.Errorf("Expected status code 401, got %v", resp.StatusCode)
96112
}
97113
}
98114

@@ -102,14 +118,19 @@ func TestMissingAuthHeader(t *testing.T) {
102118

103119
// Read example task
104120
task := readExampleTask("../example-tasks/hello-world.json")
121+
taskData, _ := json.Marshal(task)
105122

106123
// Create an Authorization Header to pass to the authorizer
107-
authHeader := http.Header{}
124+
req, _ := http.NewRequest("POST", "http://localhost:8080/s3", bytes.NewBuffer(taskData))
125+
client := &http.Client{}
126+
resp, err := client.Do(req)
108127

109-
authenticator := ExampleAuthorizer{}
110-
_, err := authenticator.Authorize(authHeader, task)
128+
if err != nil {
129+
t.Fatalf("Expected no error, got %v", err)
130+
}
131+
defer resp.Body.Close()
111132

112-
if err == nil {
113-
t.Fatalf("Expected 401 Unauthorized error, got %v", err)
133+
if resp.StatusCode != http.StatusUnauthorized {
134+
t.Errorf("Expected status code 401, got %v", resp.StatusCode)
114135
}
115136
}

0 commit comments

Comments
 (0)