Skip to content
Open
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
108 changes: 108 additions & 0 deletions pkg/tkn/host_access_secret_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package tkn

import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
)

func TestHostAccessSecretEncoding(t *testing.T) {
root := moduleRoot(t)

for _, dir := range []string{"tkn", filepath.Join("tkn", "template")} {
entries, err := os.ReadDir(filepath.Join(root, dir))
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
name := entry.Name()
if !isInfraTask(name) {
continue
}
checkFile(t, filepath.Join(root, dir, name))
}
}
}

func isInfraTask(name string) bool {
return strings.HasSuffix(name, ".yaml") &&
(strings.HasPrefix(name, "infra-aws-") || strings.HasPrefix(name, "infra-azure-"))
}

func checkFile(t *testing.T, path string) {
t.Helper()

sanitizeFields := map[string]struct{}{
"host": {},
"username": {},
"bastion-host": {},
"bastion-username": {},
"adminusername": {},
}
preserveFields := map[string]struct{}{
"id_rsa": {},
"bastion-id_rsa": {},
"userpassword": {},
"adminuserpassword": {},
}

f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatal(err)
}
}()

sc := bufio.NewScanner(f)
for sc.Scan() {
line := sc.Text()
if !strings.Contains(line, "$(cat /opt/host-info/") {
continue
}
field, ok := hostInfoField(line)
if !ok {
continue
}
if _, ok := preserveFields[field]; ok {
if strings.Contains(line, "tr -d") {
t.Errorf("%s: %s must not use tr -d: %s", path, field, strings.TrimSpace(line))
}
continue
}
if _, ok := sanitizeFields[field]; ok && !strings.Contains(line, `tr -d '\n\r'`) {
t.Errorf("%s: %s must use tr -d: %s", path, field, strings.TrimSpace(line))
}
}
if err := sc.Err(); err != nil {
t.Fatal(err)
}
}

func hostInfoField(line string) (string, bool) {
line = strings.TrimSpace(line)
field, _, ok := strings.Cut(line, ":")
if !ok {
return "", false
}
return field, true
}

func moduleRoot(t *testing.T) string {
t.Helper()
dir, _ := os.Getwd()
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("go.mod not found")
}
dir = parent
}
}
8 changes: 4 additions & 4 deletions tkn/infra-aws-fedora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ "$(params.airgap)" == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
8 changes: 4 additions & 4 deletions tkn/infra-aws-mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ $(params.airgap) == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
4 changes: 2 additions & 2 deletions tkn/infra-aws-rhel-ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
8 changes: 4 additions & 4 deletions tkn/infra-aws-rhel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ "$(params.airgap)" == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
8 changes: 4 additions & 4 deletions tkn/infra-aws-windows-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ $(params.airgap) == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
4 changes: 2 additions & 2 deletions tkn/infra-azure-fedora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
4 changes: 2 additions & 2 deletions tkn/infra-azure-rhel-ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
4 changes: 2 additions & 2 deletions tkn/infra-azure-rhel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
6 changes: 3 additions & 3 deletions tkn/infra-azure-windows-desktop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
userpassword: $(cat /opt/host-info/userpassword | base64 -w0)
adminusername: $(cat /opt/host-info/adminusername | base64 -w0)
adminusername: $(cat /opt/host-info/adminusername | tr -d '\n\r' | base64 -w0)
adminuserpassword: $(cat /opt/host-info/adminuserpassword | base64 -w0)
EOF

Expand Down
8 changes: 4 additions & 4 deletions tkn/template/infra-aws-fedora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ "$(params.airgap)" == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
8 changes: 4 additions & 4 deletions tkn/template/infra-aws-mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ $(params.airgap) == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
4 changes: 2 additions & 2 deletions tkn/template/infra-aws-rhel-ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
8 changes: 4 additions & 4 deletions tkn/template/infra-aws-rhel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ "$(params.airgap)" == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
8 changes: 4 additions & 4 deletions tkn/template/infra-aws-windows-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF
if [[ $(params.airgap) == "true" ]]; then
cat <<EOF >> host-info.yaml
bastion-host: $(cat /opt/host-info/bastion_host | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | base64 -w0)
bastion-host: $(cat /opt/host-info/bastion_host | tr -d '\n\r' | base64 -w0)
bastion-username: $(cat /opt/host-info/bastion_username | tr -d '\n\r' | base64 -w0)
bastion-id_rsa: $(cat /opt/host-info/bastion_id_rsa | base64 -w0)
EOF
fi
Expand Down
4 changes: 2 additions & 2 deletions tkn/template/infra-azure-fedora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
4 changes: 2 additions & 2 deletions tkn/template/infra-azure-rhel-ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
4 changes: 2 additions & 2 deletions tkn/template/infra-azure-rhel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
EOF

Expand Down
6 changes: 3 additions & 3 deletions tkn/template/infra-azure-windows-desktop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ spec:
cat <<EOF >> host-info.yaml
type: Opaque
data:
host: $(cat /opt/host-info/host | base64 -w0)
username: $(cat /opt/host-info/username | base64 -w0)
host: $(cat /opt/host-info/host | tr -d '\n\r' | base64 -w0)
username: $(cat /opt/host-info/username | tr -d '\n\r' | base64 -w0)
id_rsa: $(cat /opt/host-info/id_rsa | base64 -w0)
userpassword: $(cat /opt/host-info/userpassword | base64 -w0)
adminusername: $(cat /opt/host-info/adminusername | base64 -w0)
adminusername: $(cat /opt/host-info/adminusername | tr -d '\n\r' | base64 -w0)
adminuserpassword: $(cat /opt/host-info/adminuserpassword | base64 -w0)
EOF

Expand Down