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

Fix random ETag values due to map ordering #1432

Merged
merged 4 commits into from
Nov 28, 2024
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
3 changes: 1 addition & 2 deletions cmd/incusd/instance_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ func instancePatch(d *Daemon, r *http.Request) response.Response {
}

// Validate the ETag
etag := []any{c.Architecture(), c.LocalConfig(), c.LocalDevices(), c.IsEphemeral(), c.Profiles()}
err = localUtil.EtagCheck(r, etag)
err = localUtil.EtagCheck(r, c.ETag())
if err != nil {
return response.PreconditionFailed(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/incusd/instance_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func instancePut(d *Daemon, r *http.Request) response.Response {
}

// Validate the ETag
etag := []any{inst.Architecture(), inst.LocalConfig(), inst.LocalDevices(), inst.IsEphemeral(), inst.Profiles()}
err = localUtil.EtagCheck(r, etag)
err = localUtil.EtagCheck(r, inst.ETag())
if err != nil {
return response.PreconditionFailed(err)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/server/instance/drivers/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1589,3 +1589,26 @@ func (d *common) processStartedAt(pid int) (time.Time, error) {

return time.Unix(int64(linuxInfo.Ctim.Sec), int64(linuxInfo.Ctim.Nsec)), nil
}

// ETag returns the instance configuration ETag data for pre-condition validation.
func (d *common) ETag() []any {
if d.IsSnapshot() {
return []any{d.expiryDate}
}

// Prepare the ETag
etag := []any{d.architecture, d.ephemeral, d.profiles, d.localDevices.Sorted()}

configKeys := make([]string, 0, len(d.localConfig))
for k := range d.localConfig {
configKeys = append(configKeys, k)
}

sort.Strings(configKeys)

for _, k := range configKeys {
etag = append(etag, fmt.Sprintf("%s=%s", k, d.localConfig[k]))
}

return etag
}
12 changes: 4 additions & 8 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3531,9 +3531,7 @@ func (d *lxc) Render(options ...func(response any) error) (any, any, error) {
}

if d.IsSnapshot() {
// Prepare the ETag
etag := []any{d.expiryDate}

// Prepare the response.
snapState := api.InstanceSnapshot{
CreatedAt: d.creationDate,
ExpandedConfig: d.expandedConfig,
Expand All @@ -3558,12 +3556,10 @@ func (d *lxc) Render(options ...func(response any) error) (any, any, error) {
}
}

return &snapState, etag, nil
return &snapState, d.ETag(), nil
}

// Prepare the ETag
etag := []any{d.architecture, d.localConfig, d.localDevices, d.ephemeral, d.profiles}

// Prepare the response.
statusCode := d.statusCode()
instState := api.Instance{
ExpandedConfig: d.expandedConfig,
Expand Down Expand Up @@ -3593,7 +3589,7 @@ func (d *lxc) Render(options ...func(response any) error) (any, any, error) {
}
}

return &instState, etag, nil
return &instState, d.ETag(), nil
}

// RenderFull renders the full state of the instance.
Expand Down
12 changes: 4 additions & 8 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7865,9 +7865,7 @@ func (d *qemu) Render(options ...func(response any) error) (any, any, error) {
}

if d.IsSnapshot() {
// Prepare the ETag
etag := []any{d.expiryDate}

// Prepare the response.
snapState := api.InstanceSnapshot{
CreatedAt: d.creationDate,
ExpandedConfig: d.expandedConfig,
Expand All @@ -7892,13 +7890,11 @@ func (d *qemu) Render(options ...func(response any) error) (any, any, error) {
}
}

return &snapState, etag, nil
return &snapState, d.ETag(), nil
}

// Prepare the ETag
etag := []any{d.architecture, d.localConfig, d.localDevices, d.ephemeral, d.profiles}
// Prepare the response.
statusCode := d.statusCode()

instState := api.Instance{
ExpandedConfig: d.expandedConfig,
ExpandedDevices: d.expandedDevices.CloneNative(),
Expand Down Expand Up @@ -7927,7 +7923,7 @@ func (d *qemu) Render(options ...func(response any) error) (any, any, error) {
}
}

return &instState, etag, nil
return &instState, d.ETag(), nil
}

// RenderFull returns all info about the instance.
Expand Down
2 changes: 2 additions & 0 deletions internal/server/instance/instance_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type Instance interface {
ExpiryDate() time.Time
FillNetworkDevice(name string, m deviceConfig.Device) (deviceConfig.Device, error)

ETag() []any

// Paths.
Path() string
ExecOutputPath() string
Expand Down