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

when keys not found in redis, return NotFoundError #126

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
24 changes: 21 additions & 3 deletions pkg/driver/redis/device_manager_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ func (d *DeviceManager) OnboardGet(cn string) (*x509.Certificate, []string, erro
}

s, err := d.client.HGet(onboardSerialsHash, cn).Result()
if err != nil && errors.Is(err, redis.Nil) {
return nil, nil, &common.NotFoundError{Err: fmt.Sprintf("device not found %s", cn)}
}
if err != nil {
return nil, nil, fmt.Errorf("error reading onboard serials for %s: %v", cn, err)
}
Expand Down Expand Up @@ -392,6 +395,9 @@ func (d *DeviceManager) DeviceGet(u *uuid.UUID) (*x509.Certificate, *x509.Certif
}

serial, err := d.client.HGet(deviceSerialsHash, u.String()).Result()
if err != nil && errors.Is(err, redis.Nil) {
return nil, nil, "", &common.NotFoundError{Err: fmt.Sprintf("device not found %s", u)}
}
if err != nil {
return nil, nil, "", fmt.Errorf("error reading device serial for %s: %v", u.String(), err)
}
Expand Down Expand Up @@ -658,6 +664,9 @@ func (d *DeviceManager) GetCerts(u uuid.UUID) ([]byte, error) {
// hold our config
var b []byte
data, err := d.client.HGet(deviceAttestCertsHash, u.String()).Result()
if err != nil && errors.Is(err, redis.Nil) {
return nil, &common.NotFoundError{Err: fmt.Sprintf("no attestation certificates found for device %s", u)}
}
if err != nil {
return nil, fmt.Errorf("failed to get attest for %s: %v", u.String(), err)
} else {
Expand Down Expand Up @@ -699,6 +708,9 @@ func (d *DeviceManager) GetStorageKeys(u uuid.UUID) ([]byte, error) {
// hold our config
var b []byte
data, err := d.client.HGet(deviceStorageKeysHash, u.String()).Result()
if err != nil && errors.Is(err, redis.Nil) {
return nil, &common.NotFoundError{Err: fmt.Sprintf("no storage keys found for device %s", u)}
}
if err != nil {
return nil, fmt.Errorf("failed to get storage keys for %s: %v", u.String(), err)
} else {
Expand All @@ -713,7 +725,8 @@ func (d *DeviceManager) GetConfig(u uuid.UUID) ([]byte, error) {
// hold our config
var b []byte
data, err := d.client.HGet(deviceConfigsHash, u.String()).Result()
if err != nil {
switch {
case err != nil && errors.Is(err, redis.Nil):
// if config doesn't exist - create an empty one
b = common.CreateBaseConfig(u)
if _, err = d.client.HSet(deviceConfigsHash, u.String(), string(b)).Result(); err == nil {
Expand All @@ -722,7 +735,9 @@ func (d *DeviceManager) GetConfig(u uuid.UUID) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to save config for %s: %v", u.String(), err)
}
} else {
case err != nil:
return nil, fmt.Errorf("failed to get config for %s: %v", u.String(), err)
default:
b = []byte(data)
}

Expand Down Expand Up @@ -818,10 +833,13 @@ func (d *DeviceManager) refreshCache() error {
certStr := string(cert.Raw)

v, err := d.client.HGet(onboardSerialsHash, u).Result()
if err != nil {
if err != nil && errors.Is(err, redis.Nil) {
log.Printf("unabled to get a serial for %s: %v", u, err)
continue
}
if err != nil {
return fmt.Errorf("error getting serials for %s: %v", u, err)
}

onboardCerts[certStr] = make(map[string]bool)

Expand Down
Loading