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
17 changes: 3 additions & 14 deletions cli/config/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ package config

import (
"fmt"
"os/exec"
"regexp"

"github.com/google/glazier/go/registry"
"github.com/google/winops/powershell"
)

var (
// Dependency injection for testing.
powershellCmd = powershell
powershellCmd = powershell.Command

// IsElevatedCmd injects the command to determine the elevation state of the
// user context.
Expand All @@ -43,8 +43,7 @@ var (
// isAdmin determines if the current user is running the binary with elevated
// permissions on Windows.
func isAdmin() (bool, error) {
psBlock := `(([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match 'S-1-5-32-544')`
out, err := powershellCmd(psBlock)
out, err := powershellCmd(fmt.Sprintf("powershell.exe -File '%s'", adminScriptPath), nil, &powershell.PSConfig{ErrAction: powershell.Stop})
if err != nil {
return false, fmt.Errorf("%w: %v", errElevation, err)
}
Expand All @@ -65,13 +64,3 @@ func HasWritePermissions() error {
}
return nil
}

// Powershell represents the OS command used to run a powershell cmdlet on
// Windows.
func powershell(psBlock string) ([]byte, error) {
out, err := exec.Command("powershell.exe", "-NoProfile", "-Command", psBlock).CombinedOutput()
if err != nil {
return []byte{}, fmt.Errorf(`exec.Command("powershell.exe", "-NoProfile", "-Command", %s) command returned: %q: %v`, psBlock, out, err)
}
return out, nil
}
11 changes: 7 additions & 4 deletions cli/config/config_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows
// +build windows

package config

import (
"errors"
"testing"

"github.com/google/winops/powershell"
)

func TestIsAdmin(t *testing.T) {
Expand All @@ -31,25 +34,25 @@ func TestIsAdmin(t *testing.T) {

tests := []struct {
desc string
fakePSCmd func(string) ([]byte, error)
fakePSCmd func(string, []string, *powershell.PSConfig) ([]byte, error)
want bool
err error
}{
{
desc: "powershell error",
fakePSCmd: func(string) ([]byte, error) { return nil, errElevation },
fakePSCmd: func(string, []string, *powershell.PSConfig) ([]byte, error) { return nil, errElevation },
want: false,
err: errElevation,
},
{
desc: "is not admin",
fakePSCmd: func(string) ([]byte, error) { return outNotAdmin, nil },
fakePSCmd: func(string, []string, *powershell.PSConfig) ([]byte, error) { return outNotAdmin, nil },
want: false,
err: nil,
},
{
desc: "is admin",
fakePSCmd: func(string) ([]byte, error) { return outIsAdmin, nil },
fakePSCmd: func(string, []string, *powershell.PSConfig) ([]byte, error) { return outIsAdmin, nil },
want: true,
err: nil,
},
Expand Down
7 changes: 6 additions & 1 deletion cli/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package config

import "fmt"
import (
"fmt"
"os"
"path/filepath"
)

// distributions configures the options for different operating system
// installers.
Expand Down Expand Up @@ -69,4 +73,5 @@ var (

// ErrUSBwriteAccess contains the Error message visible to users when USB write access if forbidden.
ErrUSBwriteAccess = fmt.Errorf("contact IT helpdesk for help")
adminScriptPath = filepath.Join(os.Getenv("programfiles"), `\fresnel\scripts\check-admin.ps1`)
)
15 changes: 15 additions & 0 deletions cli/scripts/check-admin.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

(([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match 'S-1-5-32-544')