Skip to content

Commit 0209343

Browse files
committed
support project api key instead of user key for packet
1 parent 8893a6a commit 0209343

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

auth.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ type PacketAuth struct {
1313

1414
// Verify that a given Auth payload has access to a Packet account
1515
func (p *PacketAuth) Verify() bool {
16-
client := packet.NewClientWithAuth("", p.Payload, nil)
17-
user, _, err := client.Users.Current()
16+
projectID, err := GetPacketProjectFromAuthPayload(p.Payload)
1817
if err != nil {
1918
return false
2019
}
21-
return user.ID != ""
20+
return projectID != ""
2221
}
2322

2423
// CanManageInstance verifies that the passed in authentication can manage the specified instance, if it's a Packet instance

main.go

-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ func main() {
8787
log.Fatalf("CONSUL_ENCRYPT env not provided")
8888
}
8989

90-
if PacketProjectID == "" {
91-
log.Fatalf("PACKET_PROJECT_ID env not provided")
92-
}
93-
9490
consulCli, err := consul.NewClient(consulClientConfig)
9591
if err != nil {
9692
log.Fatalf("failed to setup consul client on gRPC server")

packet.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"io/ioutil"
7-
"os"
87

98
"github.com/google/uuid"
109
consul "github.com/hashicorp/consul/api"
@@ -13,10 +12,20 @@ import (
1312
packet "github.com/packethost/packngo"
1413
)
1514

16-
var (
17-
// PacketProjectID is the packet project id where instances should be created. Figure this out...
18-
PacketProjectID = os.Getenv("PACKET_PROJECT_ID")
19-
)
15+
// GetPacketProjectFromAuthPayload returns the Packet project of a project level API key
16+
func GetPacketProjectFromAuthPayload(auth string) (string, error) {
17+
packetClient := packet.NewClientWithAuth("", auth, nil)
18+
var project map[string]interface{}
19+
_, err := packetClient.DoRequest("GET", "/project", "", &project)
20+
if err != nil {
21+
return "", err
22+
}
23+
projectID, ok := project["id"]
24+
if !ok {
25+
return "", errors.New("problem verifying project from auth")
26+
}
27+
return projectID.(string), nil
28+
}
2029

2130
// GetPacketInstance gets an instance by ID
2231
func GetPacketInstance(consulClient *consul.Client, in *pb.GetInstanceRequest) (*pb.Instance, error) {
@@ -38,14 +47,12 @@ func CreatePacketInstance(consulClient *consul.Client, in *pb.CreateInstanceRequ
3847
id := uuid.New()
3948

4049
packetClient := packet.NewClientWithAuth("", in.Auth.Payload, nil)
41-
_, _, err := packetClient.Users.Current()
50+
51+
projID, err := GetPacketProjectFromAuthPayload(in.Auth.Payload)
4252
if err != nil {
4353
return nil, err
4454
}
4555

46-
// TODO: figure this out...
47-
projID := PacketProjectID
48-
4956
instance, err := instance.CreateInstance(consulClient, instance.CreateInstanceRequest{
5057
ID: id.String(),
5158
Owner: projID,
@@ -88,7 +95,7 @@ func CreatePacketInstance(consulClient *consul.Client, in *pb.CreateInstanceRequ
8895
Hostname: "open-copilot-instance-" + id.String(),
8996
ProjectID: projID,
9097
Facility: "ewr1",
91-
Plan: "baremetal_2",
98+
Plan: "baremetal_1",
9299
OS: "ubuntu_16_04",
93100
BillingCycle: "hourly",
94101
CustomData: string(customDataJSON),

0 commit comments

Comments
 (0)