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
417package utility
518
619import (
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
6032func HasRootPermissions (folderPath string ) (bool , error ) {
0 commit comments