Skip to content

Commit a36b941

Browse files
committed
fix switching to isc manager user
when switching to the manager user make sure that: - we aren't already the user in question - we are able to switch users (we are root)
1 parent 643e92e commit a36b941

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

.github/actions/test/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: tests isclib
33
inputs:
44
go-version:
55
description: "the version of golang"
6-
default: '1.21'
6+
default: '1.22'
77
runs:
88
using: composite
99
steps:

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ jobs:
99
steps:
1010
- uses: actions/setup-go@v3
1111
with:
12-
go-version: '1.21'
12+
go-version: '1.22'
1313
- uses: actions/checkout@v3
1414
- name: golangci-lint
1515
uses: golangci/golangci-lint-action@v3
1616
build:
1717
runs-on: ubuntu-latest
1818
strategy:
1919
matrix:
20-
version: ['1.20', '1.21']
20+
version: ['1.20', '1.21', '1.22']
2121
name: Go ${{ matrix.version }}
2222
steps:
2323
- uses: actions/checkout@v2

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ module github.com/ontariosystems/isclib/v2
33
go 1.20
44

55
require (
6-
github.com/onsi/ginkgo/v2 v2.15.0
7-
github.com/onsi/gomega v1.31.1
6+
github.com/onsi/ginkgo/v2 v2.17.1
7+
github.com/onsi/gomega v1.32.0
88
github.com/sirupsen/logrus v1.9.3
99
github.com/spf13/afero v1.11.0
1010
)
1111

1212
require (
13-
github.com/go-logr/logr v1.3.0 // indirect
13+
github.com/go-logr/logr v1.4.1 // indirect
1414
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
1515
github.com/google/go-cmp v0.6.0 // indirect
1616
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
17-
golang.org/x/net v0.19.0 // indirect
18-
golang.org/x/sys v0.17.0 // indirect
17+
golang.org/x/net v0.20.0 // indirect
18+
golang.org/x/sys v0.18.0 // indirect
1919
golang.org/x/text v0.14.0 // indirect
20-
golang.org/x/tools v0.16.1 // indirect
20+
golang.org/x/tools v0.17.0 // indirect
2121
gopkg.in/yaml.v3 v3.0.1 // indirect
2222
)

go.sum

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
8-
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
7+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
8+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
99
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
1010
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
11-
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
1211
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1312
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1413
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
1514
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
1615
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
17-
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
18-
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
19-
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
20-
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
16+
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
17+
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
18+
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
19+
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
2120
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2221
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2322
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
@@ -28,17 +27,17 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
2827
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2928
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3029
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
31-
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
32-
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
30+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
31+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
3332
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3433
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35-
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
36-
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
34+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
35+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3736
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
3837
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
39-
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
40-
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
41-
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
38+
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
39+
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
40+
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
4241
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4342
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4443
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

instance.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,19 @@ func (i *Instance) managerSysProc() (*syscall.SysProcAttr, error) {
263263
return nil, err
264264
}
265265

266-
uid, gid, err := lookupUser(mgr)
266+
sysProcAttr, err := switchUserSysProc(mgr)
267267
if err != nil {
268268
return nil, err
269269
}
270270

271-
log.WithFields(log.Fields{"user": mgr, "uid": uid, "gid": gid}).Debug("instance manager sysproc")
272-
return &syscall.SysProcAttr{
273-
Credential: &syscall.Credential{
274-
Uid: uint32(uid),
275-
Gid: uint32(gid),
276-
},
277-
}, nil
271+
if sysProcAttr != nil && sysProcAttr.Credential != nil {
272+
log.WithFields(log.Fields{
273+
"user": mgr,
274+
"uid": sysProcAttr.Credential.Uid,
275+
"gid": sysProcAttr.Credential.Gid,
276+
}).Debug("instance manager sysproc")
277+
}
278+
return sysProcAttr, nil
278279
}
279280

280281
// DetermineOwner will determine the owner of an instance by reader the parameters file associate with this instance.
@@ -454,28 +455,43 @@ func (i *Instance) ExecuteAsManager() error {
454455
// This command only functions if the calling program is running as root.
455456
// It returns any error encountered.
456457
func (i *Instance) ExecuteAsUser(execUser string) error {
458+
sysProcAttr, err := switchUserSysProc(execUser)
459+
if err != nil {
460+
return err
461+
}
462+
if sysProcAttr != nil && sysProcAttr.Credential != nil {
463+
log.WithFields(log.Fields{
464+
"user": execUser,
465+
"uid": sysProcAttr.Credential.Uid,
466+
"gid": sysProcAttr.Credential.Gid,
467+
}).Debug("Configured to execute as alternate user")
468+
}
469+
i.executionSysProcAttr = sysProcAttr
470+
return nil
471+
}
472+
473+
func switchUserSysProc(execUser string) (*syscall.SysProcAttr, error) {
457474
// no need to switch users if we're already who we want to be
458475
if err := checkUser(execUser); err == nil {
459-
return nil
476+
return &syscall.SysProcAttr{}, nil
460477
}
461478

479+
// if we're not root, we won't be able to switch to someone else
462480
if err := checkUser("root"); err != nil {
463-
return err
481+
return nil, err
464482
}
465483

466484
uid, gid, err := lookupUser(execUser)
467485
if err != nil {
468-
return err
486+
return nil, err
469487
}
470488

471-
log.WithFields(log.Fields{"user": execUser, "uid": uid, "gid": gid}).Debug("Configured to execute as alternate user")
472-
i.executionSysProcAttr = &syscall.SysProcAttr{
489+
return &syscall.SysProcAttr{
473490
Credential: &syscall.Credential{
474491
Uid: uint32(uid),
475492
Gid: uint32(gid),
476493
},
477-
}
478-
return nil
494+
}, nil
479495
}
480496

481497
func lookupUser(execUser string) (uid, gid uint64, err error) {

0 commit comments

Comments
 (0)