Skip to content

Commit ec670ac

Browse files
VishnuKarthikRavindranadchalla
authored andcommitted
Increase timeouts in SSM-Setup-CLI
cr: https://code.amazon.com/reviews/CR-122260975
1 parent 795d70e commit ec670ac

File tree

12 files changed

+245
-107
lines changed

12 files changed

+245
-107
lines changed

agent/cli/clicommand/getdiagnostics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/aws/amazon-ssm-agent/agent/cli/cliutil"
2828
_ "github.com/aws/amazon-ssm-agent/agent/cli/diagnostics"
2929
"github.com/aws/amazon-ssm-agent/agent/cli/diagnosticsutil"
30+
"github.com/aws/amazon-ssm-agent/common/utility"
3031
)
3132

3233
const (
@@ -285,7 +286,7 @@ func (c *GetDiagnosticsCommand) Execute(subcommands []string, parameters map[str
285286
}
286287

287288
// Check if cli is running as admin/root
288-
err = diagnosticsutil.IsRunningElevatedPermissions()
289+
err = utility.IsRunningElevatedPermissions()
289290
if err != nil {
290291
return nil, err.Error()
291292
}

agent/cli/diagnosticsutil/diagnosticsutil_darwin.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package diagnosticsutil
1818

1919
import (
2020
"fmt"
21-
"os/user"
2221
"time"
2322

2423
"github.com/aws/amazon-ssm-agent/agent/log/logger"
@@ -34,20 +33,6 @@ const (
3433
newlineCharacter = "\n"
3534
)
3635

37-
// IsRunningElevatedPermissions checks if the ssm-cli is being executed as administrator
38-
func IsRunningElevatedPermissions() error {
39-
currentUser, err := user.Current()
40-
if err != nil {
41-
return err
42-
}
43-
44-
if currentUser.Username == ExpectedServiceRunningUser {
45-
return nil
46-
} else {
47-
return fmt.Errorf("get-diagnostics needs to be executed by %s", ExpectedServiceRunningUser)
48-
}
49-
}
50-
5136
// AssumeAgentEnvironmentProxy is a noop on darwin because there is no other special proxy configuration
5237
func AssumeAgentEnvironmentProxy() {
5338
proxyconfig.SetProxyConfig(logger.NewSilentLogger())

agent/cli/diagnosticsutil/diagnosticsutil_unix.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24-
"os/user"
2524
"path"
2625
"path/filepath"
2726
"strings"
@@ -39,20 +38,6 @@ const (
3938
newlineCharacter = "\n"
4039
)
4140

42-
// IsRunningElevatedPermissions checks if the ssm-cli is being executed as administrator
43-
func IsRunningElevatedPermissions() error {
44-
currentUser, err := user.Current()
45-
if err != nil {
46-
return err
47-
}
48-
49-
if currentUser.Username == ExpectedServiceRunningUser {
50-
return nil
51-
} else {
52-
return fmt.Errorf("get-diagnostics needs to be executed by %s", ExpectedServiceRunningUser)
53-
}
54-
}
55-
5641
// AssumeAgentEnvironmentProxy reads the amazon-ssm-agent environment variables and assumes the same proxy settings
5742
func AssumeAgentEnvironmentProxy() {
5843
pid, err := getRunningAgentPid()

agent/cli/diagnosticsutil/diagnosticsutil_windows.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737

3838
// newlineCharacter is the system specific newline character
3939
newlineCharacter = "\r\n"
40+
41+
defaultCommandTimeOut = 30 * time.Second
4042
)
4143

4244
var powershellArgs = []string{"-InputFormat", "None", "-Noninteractive", "-NoProfile", "-ExecutionPolicy", "unrestricted"}
@@ -52,24 +54,6 @@ func executePowershellScriptWithTimeout(timeout time.Duration, scriptPath string
5254
return ExecuteCommandWithTimeout(timeout, appconfig.PowerShellPluginCommandName, args...)
5355
}
5456

55-
// IsRunningElevatedPermissions checks if the ssm-cli is being executed as administrator
56-
func IsRunningElevatedPermissions() error {
57-
checkAdminCmd := `([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')`
58-
output, err := executePowershellCommandWithTimeout(2*time.Second, checkAdminCmd)
59-
60-
if err != nil {
61-
return fmt.Errorf("failed to check permissions: %s", err)
62-
}
63-
64-
if output == "True" {
65-
return nil
66-
} else if output == "False" {
67-
return fmt.Errorf("get-diagnostics needs to be executed by administrator")
68-
} else {
69-
return fmt.Errorf("unexpected permission check output: %s", output)
70-
}
71-
}
72-
7357
func isHttpOrHttpsProxyConfigured(proxyEnv map[string]string) bool {
7458
_, ishttpSet := proxyEnv[proxyconfig.PROXY_VAR_HTTP]
7559
_, ishttpsSet := proxyEnv[proxyconfig.PROXY_VAR_HTTPS]

agent/setupcli/managers/downloadmanager/downloadmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (d *downloadManager) readVersionFromURL(versionURL string) (string, error)
246246

247247
var content string
248248
err = backOffRetry(func() error {
249-
httpTimeout := 15 * time.Second
249+
httpTimeout := 30 * time.Second
250250
tr := network.GetDefaultTransport(d.log, appconfig.DefaultConfig())
251251
client := &http.Client{
252252
Transport: tr,

agent/setupcli/setupcli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/aws/amazon-ssm-agent/agent/setupcli/utility"
4444
agentVersioning "github.com/aws/amazon-ssm-agent/agent/version"
4545
"github.com/aws/amazon-ssm-agent/agent/versionutil"
46+
utilityCmn "github.com/aws/amazon-ssm-agent/common/utility"
4647
"github.com/aws/amazon-ssm-agent/core/executor"
4748
"github.com/cihub/seelog"
4849
)
@@ -78,7 +79,7 @@ var (
7879
getVerificationManager = managers.GetVerificationManager
7980
getDownloadManager = managers.GetDownloadManager
8081
startAgent = servicemanagers.StartAgent
81-
hasElevatedPermissions = utility.IsRunningElevatedPermissions
82+
hasElevatedPermissions = utilityCmn.IsRunningElevatedPermissions
8283

8384
osExecutable = os.Executable
8485
evalSymLinks = filepath.EvalSymlinks

agent/setupcli/utility/ssm_setup_cli_util_unix.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package utility
66
import (
77
"fmt"
88
"os"
9-
"os/user"
109
"syscall"
1110

1211
"github.com/aws/amazon-ssm-agent/agent/appconfig"
@@ -22,20 +21,6 @@ const (
2221
AgentBinary = appconfig.DefaultAgentName
2322
)
2423

25-
// IsRunningElevatedPermissions checks if the ssm-setup-cli is being executed as administrator
26-
func IsRunningElevatedPermissions() error {
27-
currentUser, err := user.Current()
28-
if err != nil {
29-
return err
30-
}
31-
32-
if currentUser.Username == ExpectedServiceRunningUser {
33-
return nil
34-
} else {
35-
return fmt.Errorf("ssm-setup-cli needs to be executed by %s", ExpectedServiceRunningUser)
36-
}
37-
}
38-
3924
// HasRootPermissions shows whether the folder path has root permission
4025
func HasRootPermissions(folderPath string) (bool, error) {
4126
fileInfo, err := os.Stat(folderPath)

agent/setupcli/utility/ssm_setup_cli_util_windows.go

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
4+
// use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
114
//go:build windows
215
// +build windows
316

417
package utility
518

619
import (
7-
"context"
8-
"fmt"
9-
"os/exec"
10-
"strings"
11-
"time"
12-
1320
"github.com/aws/amazon-ssm-agent/agent/appconfig"
1421
)
1522

@@ -20,41 +27,6 @@ const (
2027
AgentBinary = appconfig.DefaultAgentName + ".exe"
2128
)
2229

23-
var powershellArgs = []string{"-InputFormat", "None", "-Noninteractive", "-NoProfile", "-ExecutionPolicy", "unrestricted"}
24-
25-
// IsRunningElevatedPermissions checks if the ssm-setup-cli is being executed as administrator
26-
func IsRunningElevatedPermissions() error {
27-
checkAdminCmd := `([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')`
28-
output, err := executePowershellCommandWithTimeout(2*time.Second, checkAdminCmd)
29-
30-
if err != nil {
31-
return fmt.Errorf("failed to check permissions: %s", err)
32-
}
33-
34-
if output == "True" {
35-
return nil
36-
} else if output == "False" {
37-
return fmt.Errorf("ssm-setup-cli needs to be executed by administrator")
38-
} else {
39-
return fmt.Errorf("unexpected permission check output: %s", output)
40-
}
41-
}
42-
43-
func executePowershellCommandWithTimeout(timeout time.Duration, command string) (string, error) {
44-
args := append(powershellArgs, "-Command", command)
45-
return executeCommandWithTimeout(timeout, appconfig.PowerShellPluginCommandName, args...)
46-
}
47-
48-
func executeCommandWithTimeout(timeout time.Duration, cmd string, args ...string) (string, error) {
49-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
50-
defer cancel()
51-
52-
byteArr, err := exec.CommandContext(ctx, cmd, args...).Output()
53-
output := strings.TrimSpace(string(byteArr))
54-
55-
return output, err
56-
}
57-
5830
// HasRootPermissions shows whether the folder path has root permission
5931
// For windows, this function is will always return true as Greengrass support is not available for windows still
6032
func HasRootPermissions(folderPath string) (bool, error) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
4+
// use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
//go:build freebsd || linux || netbsd || openbsd || darwin
15+
// +build freebsd linux netbsd openbsd darwin
16+
17+
package utility
18+
19+
import (
20+
"os/user"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func Test_IsRunningElevatedPermissions_Success(t *testing.T) {
27+
userCurrent = func() (*user.User, error) {
28+
return &user.User{Username: ExpectedServiceRunningUser}, nil
29+
}
30+
err := IsRunningElevatedPermissions()
31+
assert.Nil(t, err)
32+
}
33+
34+
func Test_IsRunningElevatedPermissions_Failure(t *testing.T) {
35+
userCurrent = func() (*user.User, error) {
36+
return &user.User{Username: "DummyUser"}, nil
37+
}
38+
err := IsRunningElevatedPermissions()
39+
assert.NotNil(t, err)
40+
}

common/utility/utility_windows.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
4+
// use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
// either express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
//go:build windows
15+
// +build windows
16+
17+
package utility
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os/exec"
23+
"strings"
24+
"time"
25+
26+
"github.com/aws/amazon-ssm-agent/agent/appconfig"
27+
)
28+
29+
const (
30+
defaultCommandTimeOut = 30 * time.Second
31+
)
32+
33+
var (
34+
executePowershellCommandWithTimeoutFunc = executePowershellCommandWithTimeout
35+
)
36+
37+
var powershellArgs = []string{"-InputFormat", "None", "-Noninteractive", "-NoProfile", "-ExecutionPolicy", "unrestricted"}
38+
39+
// IsRunningElevatedPermissions checks if current user is administrator
40+
func IsRunningElevatedPermissions() error {
41+
checkAdminCmd := `([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')`
42+
isAdminTrue := "True"
43+
isAdminFalse := "False"
44+
45+
output, err := executePowershellCommandWithTimeoutFunc(defaultCommandTimeOut, checkAdminCmd)
46+
if err != nil {
47+
return fmt.Errorf("failed to check permissions: %v", err)
48+
}
49+
50+
if output == isAdminTrue {
51+
return nil
52+
} else if output == isAdminFalse {
53+
return fmt.Errorf("binary needs to be executed by administrator")
54+
} else {
55+
return fmt.Errorf("unexpected permission check output: %v", output)
56+
}
57+
}
58+
59+
func executePowershellCommandWithTimeout(timeout time.Duration, command string) (string, error) {
60+
args := append(powershellArgs, "-Command", command)
61+
return executeCommandWithTimeout(timeout, appconfig.PowerShellPluginCommandName, args...)
62+
}
63+
64+
func executeCommandWithTimeout(timeout time.Duration, cmd string, args ...string) (string, error) {
65+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
66+
defer cancel()
67+
68+
byteArr, err := exec.CommandContext(ctx, cmd, args...).Output()
69+
output := strings.TrimSpace(string(byteArr))
70+
71+
return output, err
72+
}

0 commit comments

Comments
 (0)