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

🐛 Disallow fetching secrets from namespaces different from the host's one #1929

Merged
merged 1 commit into from
Sep 3, 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
4 changes: 4 additions & 0 deletions controllers/metal3.io/baremetalhost_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const (
)

func newSecret(name string, data map[string]string) *corev1.Secret {
return newSecretInNamespace(name, namespace, data)
}

func newSecretInNamespace(name, namespace string, data map[string]string) *corev1.Secret {
secretData := make(map[string][]byte)
for k, v := range data {
secretData[k] = []byte(base64.StdEncoding.EncodeToString([]byte(v)))
Expand Down
5 changes: 5 additions & 0 deletions controllers/metal3.io/host_config_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/go-logr/logr"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand All @@ -20,6 +21,10 @@ type hostConfigData struct {
// parameter to detirmine which data to return in case secret contins multiple
// keys.
func (hcd *hostConfigData) getSecretData(name, namespace, dataKey string) (string, error) {
if namespace != hcd.host.Namespace {
return "", errors.Errorf("%s secret must be in same namespace as host %s/%s", dataKey, hcd.host.Namespace, hcd.host.Name)
}

key := types.NamespacedName{
Name: name,
Namespace: namespace,
Expand Down
50 changes: 49 additions & 1 deletion controllers/metal3.io/host_config_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,54 @@ func TestProvisionWithHostConfig(t *testing.T) {
ExpectedNetworkData: "",
ErrNetworkData: true,
},
{
Scenario: "user-data secret in different namespace",
Host: newHost("host-user-data",
&metal3api.BareMetalHostSpec{
BMC: metal3api.BMCDetails{
Address: "ipmi://192.168.122.1:6233",
CredentialsName: defaultSecretName,
},
UserData: &corev1.SecretReference{
Name: "user-data",
Namespace: "other-namespace",
},
}),
UserDataSecret: newSecretInNamespace("user-data", "other-namespace", map[string]string{"userData": "somedata"}),
ErrUserData: true,
},
{
Scenario: "meta-data secret in different namespace",
Host: newHost("host-user-data",
&metal3api.BareMetalHostSpec{
BMC: metal3api.BMCDetails{
Address: "ipmi://192.168.122.1:6233",
CredentialsName: defaultSecretName,
},
MetaData: &corev1.SecretReference{
Name: "meta-data",
Namespace: "other-namespace",
},
}),
NetworkDataSecret: newSecretInNamespace("meta-data", "other-namespace", map[string]string{"metaData": "key: value"}),
ErrMetaData: true,
},
{
Scenario: "network-data secret in different namespace",
Host: newHost("host-user-data",
&metal3api.BareMetalHostSpec{
BMC: metal3api.BMCDetails{
Address: "ipmi://192.168.122.1:6233",
CredentialsName: defaultSecretName,
},
NetworkData: &corev1.SecretReference{
Name: "net-data",
Namespace: "other-namespace",
},
}),
NetworkDataSecret: newSecretInNamespace("net-data", "other-namespace", map[string]string{"networkData": "key: value"}),
ErrNetworkData: true,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -378,7 +426,7 @@ func TestProvisionWithHostConfig(t *testing.T) {
}

if actualMetaData != tc.ExpectedMetaData {
t.Fatal(fmt.Errorf("Failed to assert MetaData. Expected '%s' got '%s'", actualMetaData, tc.ExpectedMetaData))
t.Fatal(fmt.Errorf("Failed to assert MetaData. Expected '%s' got '%s'", tc.ExpectedMetaData, actualMetaData))
}
})
}
Expand Down