Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uuid endpoint #39

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/driver/device_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type DeviceManager interface {
GetConfig(uuid.UUID) ([]byte, error)
// SetConfig set the config for a given uuid
SetConfig(uuid.UUID, []byte) error
// GetUUID get the UuidResponse for a given uuid
GetUUID(uuid.UUID) ([]byte, error)
// GetLogsReader get the logs for a given uuid
GetLogsReader(u uuid.UUID) (io.Reader, error)
// GetInfoReader get the info for a given uuid
Expand Down
11 changes: 11 additions & 0 deletions pkg/driver/file/device_manager_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lf-edge/adam/pkg/driver/common"
"github.com/lf-edge/adam/pkg/util"
ax "github.com/lf-edge/adam/pkg/x509"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
uuid "github.com/satori/go.uuid"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -1146,3 +1147,13 @@ func (d *DeviceManager) WriteFlowMessage(u uuid.UUID, b []byte) error {
dev := d.devices[u]
return dev.AddFlowRecord(b)
}

// GetUUID get UuidResponse for device by uuid
func (d *DeviceManager) GetUUID(u uuid.UUID) ([]byte, error) {
// check that the device actually exists
if !d.deviceExists(u) {
return nil, fmt.Errorf("unregistered device UUID: %s", u)
}
ur := &eveuuid.UuidResponse{Uuid: u.String()}
return proto.Marshal(ur)
}
51 changes: 51 additions & 0 deletions pkg/driver/file/device_manager_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"github.com/lf-edge/adam/pkg/driver/common"
"github.com/lf-edge/adam/pkg/util"
ax "github.com/lf-edge/adam/pkg/x509"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
"github.com/lf-edge/eve/api/go/info"
"github.com/lf-edge/eve/api/go/metrics"
uuid "github.com/satori/go.uuid"
"google.golang.org/protobuf/proto"
)

func TestDeviceManager(t *testing.T) {
Expand Down Expand Up @@ -738,6 +740,55 @@ func TestDeviceManager(t *testing.T) {
}
}
})

t.Run("TestGetUUID", func(t *testing.T) {
u, _ := uuid.NewV4()

tests := []struct {
deviceExists bool
}{
{false},
{true},
}
for _, tt := range tests {
// reset with each test
// make a temporary directory with which to work
dir, err := ioutil.TempDir("", "adam-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
d := DeviceManager{
databasePath: dir,
}
uids := fillDevice(&d)
switch {
case tt.deviceExists:
u = *uids[0]
}
uuidResponse, err := d.GetUUID(u)
if tt.deviceExists {
if err != nil {
t.Errorf("Error: %v", err)
}
if len(uuidResponse) == 0 {
t.Error("Empty uuidResponse")
}
var ur eveuuid.UuidResponse
err := proto.Unmarshal(uuidResponse, &ur)
if err != nil {
t.Errorf("Unmarshal error: %v", err)
}
if ur.GetUuid() != u.String() {
t.Error("uuid mismatch in uuidResponse")
}
continue
}
if err == nil {
t.Errorf("empty error for non-exist device")
}
}
})
}

func copyFile(src, dest string) error {
Expand Down
13 changes: 13 additions & 0 deletions pkg/driver/memory/device_manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"io"

"github.com/lf-edge/adam/pkg/driver/common"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
uuid "github.com/satori/go.uuid"
"google.golang.org/protobuf/proto"
)

const (
Expand Down Expand Up @@ -509,3 +511,14 @@ func (d *DeviceManager) WriteFlowMessage(u uuid.UUID, b []byte) error {
// append the messages
return dev.AddFlowRecord(b)
}

// GetUUID get UuidResponse for device by uuid
func (d *DeviceManager) GetUUID(u uuid.UUID) ([]byte, error) {
// check that the device actually exists
_, ok := d.devices[u]
if !ok {
return nil, fmt.Errorf("unregistered device UUID: %s", u)
}
ur := &eveuuid.UuidResponse{Uuid: u.String()}
return proto.Marshal(ur)
}
41 changes: 41 additions & 0 deletions pkg/driver/memory/device_manager_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

"github.com/lf-edge/adam/pkg/driver/common"
ax "github.com/lf-edge/adam/pkg/x509"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
"github.com/lf-edge/eve/api/go/info"
"github.com/lf-edge/eve/api/go/metrics"
uuid "github.com/satori/go.uuid"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func TestDeviceManager(t *testing.T) {
Expand Down Expand Up @@ -682,4 +684,43 @@ func TestDeviceManager(t *testing.T) {
}
}
})

t.Run("TestGetUUID", func(t *testing.T) {
u, _ := uuid.NewV4()
d := DeviceManager{}

tests := []struct {
deviceExists bool
}{
{false},
{true},
}
for _, tt := range tests {
d.devices = map[uuid.UUID]common.DeviceStorage{}
if tt.deviceExists {
d.devices[u] = common.DeviceStorage{}
}
uuidResponse, err := d.GetUUID(u)
if tt.deviceExists {
if err != nil {
t.Errorf("Error: %v", err)
}
if len(uuidResponse) == 0 {
t.Error("Empty uuidResponse")
}
var ur eveuuid.UuidResponse
err := proto.Unmarshal(uuidResponse, &ur)
if err != nil {
t.Errorf("Unmarshal error: %v", err)
}
if ur.GetUuid() != u.String() {
t.Error("uuid mismatch in uuidResponse")
}
continue
}
if err == nil {
t.Errorf("empty error for non-exist device")
}
}
})
}
12 changes: 12 additions & 0 deletions pkg/driver/redis/device_manager_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-redis/redis"
"github.com/lf-edge/adam/pkg/driver/common"
ax "github.com/lf-edge/adam/pkg/x509"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
uuid "github.com/satori/go.uuid"
"github.com/vmihailenco/msgpack/v4"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -993,3 +994,14 @@ func (d *DeviceManager) WriteFlowMessage(u uuid.UUID, b []byte) error {
}
return dev.AddFlowRecord(b)
}

// GetUUID get UuidResponse for device by uuid
func (d *DeviceManager) GetUUID(u uuid.UUID) ([]byte, error) {
// check that the device actually exists
_, ok := d.devices[u]
if !ok {
return nil, fmt.Errorf("unregistered device UUID: %s", u)
}
ur := &eveuuid.UuidResponse{Uuid: u.String()}
return proto.Marshal(ur)
}
12 changes: 11 additions & 1 deletion pkg/driver/redis/device_manager_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/lf-edge/adam/pkg/util"
ax "github.com/lf-edge/adam/pkg/x509"
"github.com/lf-edge/eve/api/go/config"
eveuuid "github.com/lf-edge/eve/api/go/eveuuid"
"github.com/lf-edge/eve/api/go/info"
"github.com/lf-edge/eve/api/go/metrics"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -106,14 +108,22 @@ func TestDeviceRedis(t *testing.T) {
assert.Equal(t, nil, err)
UUID, err := r.DeviceCheckCert(cert)
assert.Equal(t, nil, err)
assert.Equal(t, UUID1, UUID)
assert.Equal(t, UUID1, *UUID)

certBack, certOnboardBack, serial, err := r.DeviceGet(UUID)
assert.Equal(t, nil, err)
assert.Equal(t, "123456", serial)
assert.Equal(t, cert, certBack)
assert.Equal(t, certOnboard, certOnboardBack)

uuidResponse, err := r.GetUUID(*UUID)
assert.Equal(t, nil, err)

var ur eveuuid.UuidResponse
err = proto.Unmarshal(uuidResponse, &ur)
assert.Equal(t, nil, err)
assert.Equal(t, ur.GetUuid(), UUID.String())

UUIDs, err := r.DeviceList()
assert.Equal(t, nil, err)
assert.Equal(t, 3, len(UUIDs))
Expand Down
16 changes: 16 additions & 0 deletions pkg/server/apiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ func (h *apiHandler) appLogs(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
}

func (h *apiHandler) uuid(w http.ResponseWriter, r *http.Request) {
u := h.checkCertAndRecord(w, r)
if u == nil {
return
}
uuidResponce, err := h.manager.GetUUID(*u)
if err != nil {
log.Printf("error getting device uuidResponce: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Add(contentType, mimeProto)
w.WriteHeader(http.StatusOK)
w.Write(uuidResponce)
}

func (h *apiHandler) newAppLogs(w http.ResponseWriter, r *http.Request) {
u := h.checkCertAndRecord(w, r)
if u == nil {
Expand Down
23 changes: 23 additions & 0 deletions pkg/server/apiHandlerv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,26 @@ func (h *apiHandlerv2) flowlog(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(status)
}

func (h *apiHandlerv2) uuid(w http.ResponseWriter, r *http.Request) {
u := h.checkCertAndRecord(w, r)
if u == nil {
return
}
uuidResponce, err := h.manager.GetUUID(*u)
if err != nil {
log.Printf("error getting device uuidResponce: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
cloudEnvelope, eErr := h.prepareEnvelope(uuidResponce)
if eErr != nil {
msg := fmt.Sprintf("Error occurred while creating envelope: %v", eErr)
log.Print(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
w.Header().Add(contentType, mimeProto)
w.WriteHeader(http.StatusOK)
w.Write(cloudEnvelope)
}
2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (s *Server) Start() {
ed.HandleFunc("/flowlog", api.flowLog).Methods("POST")
ed.HandleFunc("/apps/instances/id/{uuid}/logs", api.appLogs).Methods("POST")
ed.HandleFunc("/apps/instanceid/id/{uuid}/newlogs", api.newAppLogs).Methods("POST")
ed.HandleFunc("/uuid", api.uuid).Methods("POST")

if hasApiV2 {
apiv2 := &apiHandlerv2{
Expand Down Expand Up @@ -144,6 +145,7 @@ func (s *Server) Start() {
edv2.HandleFunc("/id/{uuid}/flowlog", apiv2.flowlog).Methods("POST")
edv2.HandleFunc("/id/{uuid}/apps/instances/id/{appuuid}/logs", apiv2.appLogs).Methods("POST")
edv2.HandleFunc("/id/{uuid}/apps/instanceid/{appuuid}/newlogs", apiv2.newAppLogs).Methods("POST")
edv2.HandleFunc("/uuid", apiv2.uuid).Methods("POST")
}

// admin endpoint - custom, used to manage adam
Expand Down