Skip to content

Commit

Permalink
network: restore debian-12 netplan configuration. (#433)
Browse files Browse the repository at this point in the history
In a previous release guest-agent team introduced a code path that
removed the debian-12's netplan default configuration instead of
overriding it.

This change makes sure the default configuration is re-created and
bring it consistent to the default experience of debian-12 images.

Additionally a new configuration key was introduced to allow users
to prevent guest-agent creating the configuration:
   NetworkInterfaces.restore_debian12_netplan_config

The new configuration is set to true by default.
  • Loading branch information
dorileo authored Sep 26, 2024
1 parent b1d7d2d commit c7fcc63
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ NetworkInterfaces | setup | `false` skips network interface set
NetworkInterfaces | ip\_forwarding | `false` skips IP forwarding.
NetworkInterfaces | manage\_primary\_nic | `true` will start managing the primary NIC in addition to the secondary NICs.
NetworkInterfaces | dhcp\_command | String path for alternate dhcp executable used to enable network interfaces.
NetworkInterfaces | restore_debian12_netplan_config | `true` will create the debian-12's default netplan configuration. It's set `true` by default.
OSLogin | cert_authentication | `false` prevents guest-agent from setting up sshd's `TrustedUserCAKeys`, `AuthorizedPrincipalsCommand` and `AuthorizedPrincipalsCommandUser` configuration keys. Default value: `true`.

Setting `network_enabled` to `false` will disable generating host keys and the
Expand Down
10 changes: 6 additions & 4 deletions google_guest_agent/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dhcp_command =
ip_forwarding = true
setup = true
manage_primary_nic =
restore_debian12_netplan_config = true
[OSLogin]
cert_authentication = true
Expand Down Expand Up @@ -282,10 +283,11 @@ type MDS struct {

// NetworkInterfaces contains the configurations of NetworkInterfaces section.
type NetworkInterfaces struct {
DHCPCommand string `ini:"dhcp_command,omitempty"`
IPForwarding bool `ini:"ip_forwarding,omitempty"`
Setup bool `ini:"setup,omitempty"`
ManagePrimaryNIC bool `ini:"manage_primary_nic,omitempty"`
DHCPCommand string `ini:"dhcp_command,omitempty"`
IPForwarding bool `ini:"ip_forwarding,omitempty"`
Setup bool `ini:"setup,omitempty"`
ManagePrimaryNIC bool `ini:"manage_primary_nic,omitempty"`
RestoreDebian12NetplanConfig bool `ini:"restore_debian12_netplan_config,omitempty"`
}

// Snapshots contains the configurations of Snapshots section.
Expand Down
61 changes: 60 additions & 1 deletion google_guest_agent/network/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"context"
"fmt"
"net"
"os"

"github.com/GoogleCloudPlatform/guest-agent/google_guest_agent/cfg"
"github.com/GoogleCloudPlatform/guest-agent/google_guest_agent/osinfo"
"github.com/GoogleCloudPlatform/guest-agent/metadata"
"github.com/GoogleCloudPlatform/guest-agent/utils"
"github.com/GoogleCloudPlatform/guest-logging-go/logger"
)

Expand Down Expand Up @@ -90,7 +92,30 @@ type guestAgentSection struct {
}

const (
googleComment = "# Added by Google Compute Engine Guest Agent."
googleComment = "# Added by Google Compute Engine Guest Agent."
debian12NetplanFile = "/etc/netplan/90-default.yaml"
debian12NetplanConfig = `network:
version: 2
ethernets:
all-en:
match:
name: en*
dhcp4: true
dhcp4-overrides:
use-domains: true
dhcp6: true
dhcp6-overrides:
use-domains: true
all-eth:
match:
name: eth*
dhcp4: true
dhcp4-overrides:
use-domains: true
dhcp6: true
dhcp6-overrides:
use-domains: true
`
)

var (
Expand Down Expand Up @@ -179,6 +204,10 @@ func SetupInterfaces(ctx context.Context, config *cfg.Sections, mds *metadata.De
}
}

if err := restoreDebian12NetplanConfig(config); err != nil {
logger.Errorf("Failed to restore debian-12 netplan configuration: %v", err)
}

// Attempt to rollback any left over configuration of non active network managers.
for _, svc := range knownNetworkManagers {
if svc == activeService.manager {
Expand Down Expand Up @@ -215,6 +244,36 @@ func SetupInterfaces(ctx context.Context, config *cfg.Sections, mds *metadata.De
return nil
}

// restoreDebian12NetplanConfig recreates the default netplan configuration
// for debian-12 in case user hasn't disabled it and the running system is
// indeed a debian-12 system.
func restoreDebian12NetplanConfig(config *cfg.Sections) error {
if !config.NetworkInterfaces.RestoreDebian12NetplanConfig {
logger.Debugf("User provided configuration requested to skip debian-12 netplan configuration")
return nil
}

osDesc := osinfo.Get()
if osDesc.OS != "debian" || osDesc.Version.Major != 12 {
logger.Debugf("Not running a debian-12 system, skipping netplan configuration restore")
return nil
}

if _, err := os.Stat(debian12NetplanFile); err != nil {
if !os.IsNotExist(err) {
return err
}

if err := utils.WriteFile([]byte(debian12NetplanConfig), debian12NetplanFile, 0644); err != nil {
return fmt.Errorf("Failed to recreate default netplan config: %w", err)
}

logger.Debugf("Recreated default netplan config...")
}

return nil
}

// FallbackToDefault will attempt to rescue broken networking by rolling back
// all guest-agent modifications to the network configuration.
func FallbackToDefault(ctx context.Context) error {
Expand Down

0 comments on commit c7fcc63

Please sign in to comment.