Skip to content

Commit

Permalink
fix: prevent panic in nocloud platform code
Browse files Browse the repository at this point in the history
The bug was logical: first the check was done for one of the values to
be non-nil, and after that one of the values was assumed to be non-nil,
while it could have been nil.

While fixing that, linter figured out that raw metadata config is never
needed outside of `acquireConfig`, so this got dropped as well,
simplifying the code even more.

Fixes #9578

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
(cherry picked from commit 3a0a17a)
  • Loading branch information
smira committed Nov 13, 2024
1 parent 5c7b02d commit d4a3a2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ func (n *Nocloud) configFromCD(ctx context.Context, r state.State) (metaConfig [
}

//nolint:gocyclo
func (n *Nocloud) acquireConfig(ctx context.Context, r state.State) (metadataConfigDl, metadataNetworkConfigDl, machineConfigDl []byte, metadata *MetadataConfig, err error) {
func (n *Nocloud) acquireConfig(ctx context.Context, r state.State) (metadataNetworkConfigDl, machineConfigDl []byte, metadata *MetadataConfig, err error) {
s, err := smbios.GetSMBIOSInfo()
if err != nil {
return nil, nil, nil, nil, err
return nil, nil, nil, err
}

var (
Expand Down Expand Up @@ -252,6 +252,8 @@ func (n *Nocloud) acquireConfig(ctx context.Context, r state.State) (metadataCon
}
}

var metadataConfigDl []byte

if networkSource && metaBaseURL != "" {
metadataConfigDl, metadataNetworkConfigDl, machineConfigDl, err = n.configFromNetwork(ctx, metaBaseURL, r)
} else {
Expand All @@ -277,7 +279,7 @@ func (n *Nocloud) acquireConfig(ctx context.Context, r state.State) (metadataCon
metadata.InternalDNS = fallbackMetadata.InternalDNS
}

return metadataConfigDl, metadataNetworkConfigDl, machineConfigDl, metadata, err
return metadataNetworkConfigDl, machineConfigDl, metadata, err
}

//nolint:gocyclo,cyclop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (n *Nocloud) ParseMetadata(unmarshalledNetworkConfig *NetworkConfig, st sta

// Configuration implements the runtime.Platform interface.
func (n *Nocloud) Configuration(ctx context.Context, r state.State) ([]byte, error) {
_, _, machineConfigDl, _, err := n.acquireConfig(ctx, r) //nolint:dogsled
_, machineConfigDl, _, err := n.acquireConfig(ctx, r)
if err != nil {
return nil, err
}
Expand All @@ -108,7 +108,7 @@ func (n *Nocloud) KernelArgs(string) procfs.Parameters {

// NetworkConfiguration implements the runtime.Platform interface.
func (n *Nocloud) NetworkConfiguration(ctx context.Context, st state.State, ch chan<- *runtime.PlatformNetworkConfig) error {
metadataConfigDl, metadataNetworkConfigDl, _, metadata, err := n.acquireConfig(ctx, st)
metadataNetworkConfigDl, _, metadata, err := n.acquireConfig(ctx, st)
if stderrors.Is(err, errors.ErrNoConfigSource) {
err = nil
}
Expand All @@ -117,20 +117,14 @@ func (n *Nocloud) NetworkConfiguration(ctx context.Context, st state.State, ch c
return err
}

if metadataConfigDl == nil && metadataNetworkConfigDl == nil {
if metadataNetworkConfigDl == nil {
// no data, use cached network configuration if available
return nil
}

var unmarshalledNetworkConfig *NetworkConfig

if metadataNetworkConfigDl != nil {
nc, err := DecodeNetworkConfig(metadataNetworkConfigDl)
if err != nil {
return err
}

unmarshalledNetworkConfig = nc
unmarshalledNetworkConfig, err := DecodeNetworkConfig(metadataNetworkConfigDl)
if err != nil {
return err
}

networkConfig, err := n.ParseMetadata(unmarshalledNetworkConfig, st, metadata)
Expand Down

0 comments on commit d4a3a2b

Please sign in to comment.