Skip to content

Commit ada523b

Browse files
committed
Add support for configurable endpoints
1 parent e910645 commit ada523b

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"example.com/shared"
1010
)
1111

12-
func run(user string, dir string) (string, error) {
12+
func run(user string, host string, dir string) (string, error) {
1313
m := &shared.Manager{}
1414
defer m.Close()
1515

@@ -18,7 +18,7 @@ func run(user string, dir string) (string, error) {
1818
return "", fmt.Errorf("failed to get client: %w", err)
1919
}
2020

21-
resp, err := authorize.Get(user, "http://localhost:8080/token?user=")
21+
resp, err := authorize.Get(user, host)
2222
if err != nil {
2323
return "", fmt.Errorf("failed to authorize: %w", err)
2424
}
@@ -33,12 +33,17 @@ func run(user string, dir string) (string, error) {
3333
}
3434

3535
func main() {
36-
if len(os.Args) < 2 {
37-
fmt.Printf("Usage: %s <user>\n", os.Args[0])
36+
if len(os.Args) < 3 {
37+
fmt.Printf("Usage: %s <USER> <HOST>\n", os.Args[0])
3838
os.Exit(1)
3939
}
4040

41-
out, err := run(os.Args[1], "build/plugins")
41+
user := os.Args[1]
42+
// "http://localhost:8080/token?user="
43+
host := os.Args[2]
44+
dir := "build/plugins"
45+
46+
out, err := run(user, host, dir)
4247
if err != nil {
4348
fmt.Println("Error calling plugin:", err)
4449
os.Exit(1)

plugin/auth_impl.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import (
1616
type Authorize struct{}
1717

1818
func (Authorize) Get(user string, host string) ([]byte, error) {
19+
shared.Logger.Info("Get", "user", user, "host", host)
20+
1921
if user == "" {
20-
return nil, fmt.Errorf("user is required (e.g. ./authorize <user>)")
22+
return nil, fmt.Errorf("user is required (e.g. ./authorize <USER> <HOST>)")
2123
}
22-
// Currently hardcoding the endpoint of the token service
23-
// TODO: This should be made configurable similar to the test server (see tests/test-server/main.go)
24+
2425
resp, err := http.Get(host + user)
2526
if err != nil {
2627
return nil, fmt.Errorf("error making request: %w", err)

shared/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var Handshake = plugin.HandshakeConfig{
3333
// Create an hclog.Logger
3434
var Logger = hclog.New(&hclog.LoggerOptions{
3535
Name: "plugin",
36-
Level: hclog.Warn,
36+
Level: hclog.Info,
3737
})
3838

3939
// PluginMap is the map of plugins we can dispense.

shared/rpc.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package shared
22

33
import (
4+
"fmt"
45
"net/rpc"
56
)
67

78
// RPCClient is an implementation of Authorization that talks over RPC.
89
type RPCClient struct{ client *rpc.Client }
910

10-
func (m *RPCClient) Get(user string) ([]byte, error) {
11+
func (m *RPCClient) Get(user string, host string) ([]byte, error) {
1112
var resp []byte
12-
err := m.client.Call("Plugin.Get", user, &resp)
13+
err := m.client.Call("Plugin.Get", []string{user, host}, &resp)
1314
return resp, err
1415
}
1516

@@ -20,7 +21,11 @@ type RPCServer struct {
2021
Impl Authorize
2122
}
2223

23-
func (m *RPCServer) Get(user string, host string, resp *[]byte) error {
24+
func (m *RPCServer) Get(args []string, resp *[]byte) error {
25+
if len(args) != 2 {
26+
return fmt.Errorf("expected 2 arguments, got %d", len(args))
27+
}
28+
user, host := args[0], args[1]
2429
v, err := m.Impl.Get(user, host)
2530
*resp = v
2631
return err

0 commit comments

Comments
 (0)