ROOTPLOIT
Server: LiteSpeed
System: Linux in-mum-web1878.main-hosting.eu 5.14.0-570.21.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 11 07:22:35 EDT 2025 x86_64
User: u435929562 (435929562)
PHP: 7.4.33
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/go/pkg/mod/github.com/aws/[email protected]/aws/session/csm_test.go
//go:build go1.7
// +build go1.7

package session

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

	"github.com/aws/aws-sdk-go/internal/sdktesting"
)

func TestSession_loadCSMConfig(t *testing.T) {
	defConfigFiles := []string{
		filepath.Join("testdata", "csm_shared_config"),
	}
	cases := map[string]struct {
		Envs        map[string]string
		ConfigFiles []string
		CSMProfile  string

		Expect csmConfig
		Err    string
	}{
		"no config": {
			Envs:        map[string]string{},
			Expect:      csmConfig{},
			ConfigFiles: defConfigFiles,
			CSMProfile:  "aws_csm_empty",
		},
		"env enabled": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED":   "true",
				"AWS_CSM_PORT":      "4321",
				"AWS_CSM_HOST":      "ahost",
				"AWS_CSM_CLIENT_ID": "client id",
			},
			Expect: csmConfig{
				Enabled:  true,
				Port:     "4321",
				Host:     "ahost",
				ClientID: "client id",
			},
		},
		"shared cfg enabled": {
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled:  true,
				Port:     "1234",
				Host:     "bar",
				ClientID: "foo",
			},
		},
		"mixed cfg, use env": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED": "true",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled: true,
			},
		},
		"mixed cfg, use env disabled": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED": "false",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled: false,
			},
		},
		"mixed cfg, use shared config": {
			Envs: map[string]string{
				"AWS_CSM_PORT": "4321",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled:  true,
				Port:     "1234",
				Host:     "bar",
				ClientID: "foo",
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			restoreFn := sdktesting.StashEnv()
			defer restoreFn()

			if len(c.CSMProfile) != 0 {
				csmProfile := csmProfileName
				defer func() {
					csmProfileName = csmProfile
				}()
				csmProfileName = c.CSMProfile
			}

			for name, v := range c.Envs {
				os.Setenv(name, v)
			}

			envCfg, err := loadEnvConfig()
			if err != nil {
				t.Fatalf("failed to load the envcfg, %v", err)
			}
			csmCfg, err := loadCSMConfig(envCfg, c.ConfigFiles)
			if len(c.Err) != 0 {
				if err == nil {
					t.Fatalf("expect error, got none")
				}
				if e, a := c.Err, err.Error(); !strings.Contains(a, e) {
					t.Errorf("expect %v in error %v", e, a)
				}
				return
			}

			if e, a := c.Expect, csmCfg; e != a {
				t.Errorf("expect %v CSM config got %v", e, a)
			}
		})
	}
}