diff --git a/cmd/appregistry-server/main.go b/cmd/appregistry-server/main.go index 80de6b364..244f43b03 100644 --- a/cmd/appregistry-server/main.go +++ b/cmd/appregistry-server/main.go @@ -12,6 +12,7 @@ import ( "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" "github.com/operator-framework/operator-registry/pkg/appregistry" + "github.com/operator-framework/operator-registry/pkg/lib/dns" "github.com/operator-framework/operator-registry/pkg/lib/log" "github.com/operator-framework/operator-registry/pkg/registry" "github.com/operator-framework/operator-registry/pkg/server" @@ -61,6 +62,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { if err != nil { logrus.WithError(err).Warn("unable to set termination log path") } + // Ensure there is a default nsswitch config + if err := dns.EnsureNsswitch(); err != nil { + return err + } kubeconfig, err := cmd.Flags().GetString("kubeconfig") if err != nil { return err diff --git a/cmd/configmap-server/main.go b/cmd/configmap-server/main.go index fa471fc0e..01336d456 100644 --- a/cmd/configmap-server/main.go +++ b/cmd/configmap-server/main.go @@ -17,6 +17,7 @@ import ( "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" + "github.com/operator-framework/operator-registry/pkg/lib/dns" "github.com/operator-framework/operator-registry/pkg/lib/log" "github.com/operator-framework/operator-registry/pkg/registry" "github.com/operator-framework/operator-registry/pkg/server" @@ -67,6 +68,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { if err != nil { logrus.WithError(err).Warn("unable to set termination log path") } + // Ensure there is a default nsswitch config + if err := dns.EnsureNsswitch(); err != nil { + return err + } kubeconfig, err := cmd.Flags().GetString("kubeconfig") if err != nil { return err diff --git a/cmd/opm/registry/serve.go b/cmd/opm/registry/serve.go index 4fab4cdd7..4834711a3 100644 --- a/cmd/opm/registry/serve.go +++ b/cmd/opm/registry/serve.go @@ -13,6 +13,7 @@ import ( "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" + "github.com/operator-framework/operator-registry/pkg/lib/dns" "github.com/operator-framework/operator-registry/pkg/lib/log" "github.com/operator-framework/operator-registry/pkg/server" "github.com/operator-framework/operator-registry/pkg/sqlite" @@ -53,6 +54,12 @@ func serveFunc(cmd *cobra.Command, args []string) error { if err != nil { logrus.WithError(err).Warn("unable to set termination log path") } + + // Ensure there is a default nsswitch config + if err := dns.EnsureNsswitch(); err != nil { + return err + } + dbName, err := cmd.Flags().GetString("database") if err != nil { return err diff --git a/cmd/registry-server/main.go b/cmd/registry-server/main.go index 48c3f8de3..9ba80bd0c 100644 --- a/cmd/registry-server/main.go +++ b/cmd/registry-server/main.go @@ -11,10 +11,10 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/reflection" - "github.com/operator-framework/operator-registry/pkg/lib/log" - "github.com/operator-framework/operator-registry/pkg/api" health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" + "github.com/operator-framework/operator-registry/pkg/lib/dns" + "github.com/operator-framework/operator-registry/pkg/lib/log" "github.com/operator-framework/operator-registry/pkg/server" "github.com/operator-framework/operator-registry/pkg/sqlite" ) @@ -60,6 +60,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error { if err != nil { logrus.WithError(err).Warn("unable to set termination log path") } + // Ensure there is a default nsswitch config + if err := dns.EnsureNsswitch(); err != nil { + return err + } dbName, err := cmd.Flags().GetString("database") if err != nil { return err diff --git a/pkg/lib/dns/nsswitch.go b/pkg/lib/dns/nsswitch.go new file mode 100644 index 000000000..0e69f7e79 --- /dev/null +++ b/pkg/lib/dns/nsswitch.go @@ -0,0 +1,28 @@ +package dns + +import ( + "io/ioutil" + "os" + "runtime" +) + +var ( + GOOS = runtime.GOOS + NsswitchContents = []byte("hosts: files dns") + NsswitchFilename = "/etc/nsswitch.conf" +) + +func EnsureNsswitch() error { + // only linux supports nsswitch + if GOOS != "linux" { + return nil + } + + // if the file already exists, don't overwrite it + _, err := os.Stat(NsswitchFilename) + if !os.IsNotExist(err) { + return nil + } + + return ioutil.WriteFile(NsswitchFilename, NsswitchContents, 0644) +} diff --git a/pkg/lib/dns/nsswitch_test.go b/pkg/lib/dns/nsswitch_test.go new file mode 100644 index 000000000..ecbb92a01 --- /dev/null +++ b/pkg/lib/dns/nsswitch_test.go @@ -0,0 +1,73 @@ +package dns + +import ( + "github.com/stretchr/testify/require" + "io/ioutil" + "os" + "testing" +) + +func TestEnsureNsswitch(t *testing.T) { + tests := []struct { + name string + goos string + existingFile bool + wantFile bool + wantErr bool + }{ + { + name: "no file", + goos: "linux", + existingFile: false, + wantFile: true, + wantErr: false, + }, + { + name: "existing file", + goos: "linux", + existingFile: true, + wantFile: false, + wantErr: false, + }, + { + name: "windows", + goos: "windows", + wantFile: false, + wantErr: false, + }, + { + name: "mac", + goos: "darwin", + wantFile: false, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + GOOS = tt.goos + // don't want to overwrite the real nsswitch + NsswitchFilename = "testfile" + + if tt.existingFile { + require.NoError(t, ioutil.WriteFile(NsswitchFilename, []byte("test"), 0644)) + } + + if err := EnsureNsswitch(); (err != nil) != tt.wantErr { + t.Errorf("EnsureNsswitch() error = %v, wantErr %v", err, tt.wantErr) + } + + if tt.wantFile { + contents, err := ioutil.ReadFile(NsswitchFilename) + require.NoError(t, err) + require.Equal(t, NsswitchContents, contents) + os.Remove(NsswitchFilename) + } + if tt.existingFile { + contents, err := ioutil.ReadFile(NsswitchFilename) + require.NoError(t, err) + require.NotEqual(t, NsswitchContents, contents) + os.Remove(NsswitchFilename) + } + }) + } +}