Skip to content

Commit

Permalink
use permissions repo
Browse files Browse the repository at this point in the history
Signed-off-by: Harrison Affel <[email protected]>
  • Loading branch information
HarrisonWAffel authored and brandond committed Jun 7, 2024
1 parent 2f01044 commit 2bd863b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 75 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ require (
github.com/onsi/ginkgo/v2 v2.16.0
github.com/onsi/gomega v1.32.0
github.com/pkg/errors v0.9.1
github.com/rancher/permissions v0.0.0-20240523180510-4001d3d637f7
github.com/rancher/wharfie v0.6.6
github.com/rancher/wins v0.1.1
github.com/rancher/wrangler/v3 v3.0.0-rc2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,8 @@ github.com/rancher/dynamiclistener v0.6.0-rc1 h1:Emwf9o7PMLdQNv4lvFx7xJKxDuDa4Y6
github.com/rancher/dynamiclistener v0.6.0-rc1/go.mod h1:BIPgJ8xFSUyuTyGvRMVt++S1qjD3+7Ptvq1TXl6hcTM=
github.com/rancher/lasso v0.0.0-20240430201833-6f3def65ffc5 h1:6K4RhfmCy7uxaw9OzCljNLfFcgD/q7SeF+/2gCQ3Tvw=
github.com/rancher/lasso v0.0.0-20240430201833-6f3def65ffc5/go.mod h1:7WkdfPEvWAdnHVioMUkhpZkshJzjDY62ocHVhcbw89M=
github.com/rancher/permissions v0.0.0-20240523180510-4001d3d637f7 h1:0Kg2SGoMeU1ll4xPi4DE0+qNHLFO/U5MwtK0WrIdK+o=
github.com/rancher/permissions v0.0.0-20240523180510-4001d3d637f7/go.mod h1:fsbs0YOsGn1ofPD5p+BuI4qDhbMbSJtTegKt6Ucna+c=
github.com/rancher/remotedialer v0.2.6-0.20201012155453-8b1b7bb7d05f/go.mod h1:dbzn9NF1JWbGEHL6Q/1KG4KFROILiY/j6wmfF1Np3fk=
github.com/rancher/remotedialer v0.3.0 h1:y1EO8JCsgZo0RcqTUp6U8FXcBAv27R+TLnWRcpvX1sM=
github.com/rancher/remotedialer v0.3.0/go.mod h1:BwwztuvViX2JrLLUwDlsYt5DiyUwHLlzynRwkZLAY0Q=
Expand Down
11 changes: 6 additions & 5 deletions pkg/cli/defaults/defaults_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"os"
"path/filepath"

k3swindows "github.com/k3s-io/k3s/pkg/agent/util/acl"
"github.com/pkg/errors"
rke2windows "github.com/rancher/rke2/pkg/windows"
"github.com/rancher/permissions/pkg/access"
"github.com/rancher/permissions/pkg/acl"
"github.com/rancher/permissions/pkg/sid"
"golang.org/x/sys/windows"
)

Expand All @@ -31,9 +32,9 @@ func createDataDir(dataDir string, perm os.FileMode) error {
return fmt.Errorf("failed to create data directory %s: %v", dataDir, err)
}

if err = rke2windows.Mkdir(dataDir, []windows.EXPLICIT_ACCESS{
k3swindows.GrantSid(windows.GENERIC_ALL, k3swindows.LocalSystemSID()),
k3swindows.GrantSid(windows.GENERIC_ALL, k3swindows.BuiltinAdministratorsSID()),
if err = acl.Mkdir(dataDir, []windows.EXPLICIT_ACCESS{
access.GrantSid(windows.GENERIC_ALL, sid.LocalSystem()),
access.GrantSid(windows.GENERIC_ALL, sid.BuiltinAdministrators()),
}...); err != nil {
return fmt.Errorf("failed to create data directory %s: %v", dataDir, err)
}
Expand Down
70 changes: 0 additions & 70 deletions pkg/windows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ import (
"net"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"text/template"
"time"
"unsafe"

"github.com/Microsoft/hcsshim"
wapi "github.com/iamacarpet/go-win64api"
"github.com/libp2p/go-netroute"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
opv1 "github.com/tigera/operator/api/v1"
"golang.org/x/sys/windows"
"k8s.io/apimachinery/pkg/util/wait"
)

Expand Down Expand Up @@ -347,70 +344,3 @@ func findInterface(ip string) (string, error) {

return "", fmt.Errorf("no interface has the ip: %s", ip)
}

// TODO: Remove the below ACL logic in favor of the rancher/permissions repository once that has been created

// Mkdir creates a directory using the given explicitAccess rules for a number of SIDs. If no windows.EXPLICIT_ACCESS
// rules are provided then the directory will inherit its ACL from the parent directory. If the specified
// directory already exists or another error is encountered, Mkdir will return false and the relevant error.
// Upon Successful creation of the directory, Mkdir will return 'true' and a nil error.
func Mkdir(name string, explicitAccess ...windows.EXPLICIT_ACCESS) error {
if name == "" {
return fmt.Errorf("must supply a directory name")
}

// check if the file already exists
_, err := os.Stat(name)
if err == nil {
return nil
}

sd, err := windows.NewSecurityDescriptor()
if err != nil {
return fmt.Errorf("failed to create security descriptor: %v", err)
}

// if we haven't been provided DACL rules
// we should defer to the parent directory
inheritACL := explicitAccess == nil
if explicitAccess != nil && len(explicitAccess) != 0 {
acl, err := windows.ACLFromEntries(explicitAccess, nil)
if err != nil {
return fmt.Errorf("failed to create ACL from explicit access entries: %v", err)
}

err = sd.SetDACL(acl, true, inheritACL)
if err != nil {
return fmt.Errorf("failed to configure DACL for security desctriptor: %v", err)
}
}

// set the protected DACL flag to prevent the DACL of the security descriptor from being modified by inheritable ACEs
// (i.e. prevent parent folders from modifying this ACL)
if !inheritACL {
err = sd.SetControl(windows.SE_DACL_PROTECTED, windows.SE_DACL_PROTECTED)
if err != nil {
return fmt.Errorf("failed to configure protected DACL for security descriptor: %v", err)
}
}

var securityAttribute windows.SecurityAttributes
securityAttribute.Length = uint32(unsafe.Sizeof(securityAttribute))
inheritHandle := 1
if !inheritACL {
inheritHandle = 0
}
securityAttribute.InheritHandle = uint32(inheritHandle)
securityAttribute.SecurityDescriptor = sd

namePntr, err := windows.UTF16PtrFromString(name)
if err != nil {
return err
}

if err = windows.CreateDirectory(namePntr, &securityAttribute); err != nil {
return fmt.Errorf("failed to create directory with custom ACE: %v", err)
}

return nil
}

0 comments on commit 2bd863b

Please sign in to comment.