Skip to content

Commit

Permalink
Merge pull request #175 from fbrissi/master
Browse files Browse the repository at this point in the history
Fix UID Windows
  • Loading branch information
danielsuguimoto authored Oct 23, 2020
2 parents 9acc566 + 2c2a23c commit 10d1c2c
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 16 deletions.
17 changes: 17 additions & 0 deletions build_artifacts.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#!/bin/bash

while [ $# -gt 0 ]; do
case "$1" in
--version )
KOOL_VERSION="$2"
shift 2
;;
-- )
shift
break
;;
*)
echo "Invalid Argument: ${1}"
exit 1
;;
esac
done

if [ -f .env ]; then
source .env
fi
Expand Down
7 changes: 1 addition & 6 deletions environment/asuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

package environment

import (
"fmt"
"os"
)

func initAsuser(envStorage EnvStorage) {
if envStorage.Get("KOOL_ASUSER") == "" {
envStorage.Set("KOOL_ASUSER", fmt.Sprintf("%d", os.Getuid()))
envStorage.Set("KOOL_ASUSER", uid())
}
}
25 changes: 25 additions & 0 deletions environment/asuser_test_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package environment

import (
"testing"
)

func TestInitAsuser(t *testing.T) {
f := NewFakeEnvStorage()
initAsuser(f)

if f.Envs["KOOL_ASUSER"] != uid() {
t.Error("failed setting current user to KOOL_ASUSER")
}
}

func TestAlreadyExistingKoolUserInitAsuser(t *testing.T) {
f := NewFakeEnvStorage()
f.Envs["KOOL_ASUSER"] = "testing_user"

initAsuser(f)

if f.Envs["KOOL_ASUSER"] != uid() {
t.Error("failed setting current user to KOOL_ASUSER")
}
}
2 changes: 1 addition & 1 deletion environment/asuser_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package environment
func initAsuser(envStorage EnvStorage) {
// under native windows defaults to using
// root inside containers for kool managed images
envStorage.Set("KOOL_ASUSER", "0")
envStorage.Set("KOOL_ASUSER", uid())
}
6 changes: 2 additions & 4 deletions environment/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package environment

import (
"fmt"
"log"
"os"
"strings"
Expand All @@ -28,9 +27,8 @@ func InitEnvironmentVariables(envStorage EnvStorage, defaultEnvValues string) {
if envStorage.Get("HOME") == "" {
envStorage.Set("HOME", homeDir)
}
if envStorage.Get("UID") == "" {
envStorage.Set("UID", fmt.Sprintf("%d", os.Getuid()))
}

initUid(envStorage)

if envStorage.Get("PWD") == "" {
workDir, err = os.Getwd()
Expand Down
3 changes: 1 addition & 2 deletions environment/env_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package environment

import (
"fmt"
"os"
"strings"
"testing"
Expand All @@ -28,7 +27,7 @@ func TestInitEnvironmentVariables(t *testing.T) {
t.Errorf("expecting $HOME value '%s', got '%s'", homeDir, envHomeDir)
}

UID := fmt.Sprintf("%d", os.Getuid())
UID := uid()

if envUID := f.Envs["UID"]; envUID != UID {
t.Errorf("expecting $UID value '%s', got '%s'", UID, envUID)
Expand Down
18 changes: 18 additions & 0 deletions environment/uid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build !windows

package environment

import (
"fmt"
"os"
)

func initUid(envStorage EnvStorage) {
if envStorage.Get("UID") == "" {
envStorage.Set("UID", uid())
}
}

func uid() string {
return fmt.Sprintf("%d", os.Getuid())
}
29 changes: 29 additions & 0 deletions environment/uid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build !windows

package environment

import (
"fmt"
"os"
"testing"
)

func TestInitUid(t *testing.T) {
f := NewFakeEnvStorage()
initUid(f)

if f.Envs["UID"] != fmt.Sprintf("%d", os.Getuid()) {
t.Error("failed setting current uid to UID")
}
}

func TestAlreadyExistingKoolUserInitUid(t *testing.T) {
f := NewFakeEnvStorage()
f.Envs["UID"] = "1000"

initUid(f)

if f.Envs["UID"] != "1000" {
t.Error("should not set new uid if it is already set")
}
}
25 changes: 25 additions & 0 deletions environment/uid_test_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package environment

import (
"testing"
)

func TestInitUid(t *testing.T) {
f := NewFakeEnvStorage()
initUid(f)

if f.Envs["UID"] != uid() {
t.Error("failed setting current uid to UID")
}
}

func TestAlreadyExistingKoolUserInitUid(t *testing.T) {
f := NewFakeEnvStorage()
f.Envs["UID"] = "1000"

initUid(f)

if f.Envs["UID"] != uid() {
t.Error("should not set new uid if it is already set")
}
}
30 changes: 30 additions & 0 deletions environment/uid_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package environment

import (
"os/user"
"regexp"
)

var sidExp = regexp.MustCompile(`.*-(?P<uid>\d+)`)

func initUid(envStorage EnvStorage) {
// under native windows defaults to using
// root inside containers for kool managed images
envStorage.Set("UID", uid())
}

func uid() string {
current, _ := user.Current()
match := sidExp.FindStringSubmatch(current.Uid)

results := map[string]string{}
for i, name := range match {
results[sidExp.SubexpNames()[i]] = name
}

if uid, ok := results["uid"]; ok {
return uid
}

return "0"
}
18 changes: 18 additions & 0 deletions environment/uid_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package environment

import (
"testing"
)

func TestUid(t *testing.T) {
if "0" == uid() {
t.Errorf("expecting $UID value '%s', got '%s'", uid(), "0")
}

// TODO Find a way to test, user.Current uses cache so it didn't work to change
//current, _ := user.Current()
//current.Uid = ""
//if "0" != uid() {
// t.Errorf("expecting $UID value '%s', got '%s'", "0", uid())
//}
}
3 changes: 0 additions & 3 deletions inno-setup/kool.iss
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ Name: en; MessagesFile: "compiler:Default.isl"
Source: "..\dist\kool.exe"; DestDir: "{autopf}\{#ApplicationGroup}\bin"; Flags: ignoreversion
Source: "kool.ico"; DestDir: "{autopf}\{#ApplicationGroup}"; Flags: ignoreversion

[Registry]
Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: "APPLICATION_NAME"; ValueData: "{autopf}\{#ApplicationGroup}\bin\kool.exe"; Flags: uninsdeletevalue

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
begin
Expand Down

0 comments on commit 10d1c2c

Please sign in to comment.