From da4119f11709477722c578f3847d0c6d67b2f023 Mon Sep 17 00:00:00 2001 From: Ben Dean Date: Fri, 26 Jul 2024 17:12:12 -0400 Subject: [PATCH] don't fail when user cannot read parameters.isc they might be a valid Cache user, but not the owner with permissions to read the parameters.isc --- go.mod | 15 +++++++-------- go.sum | 28 ++++++++++++++-------------- instance.go | 25 ++++++++++++++++++++++--- instance_test.go | 31 +++++++++++++++++++++++++++++-- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index b1f5273..49d3bb7 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/ontariosystems/isclib/v2 go 1.21 require ( - github.com/onsi/ginkgo/v2 v2.19.0 - github.com/onsi/gomega v1.33.1 + github.com/onsi/ginkgo/v2 v2.19.1 + github.com/onsi/gomega v1.34.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/afero v1.11.0 ) @@ -14,16 +14,15 @@ require ( github.com/go-logr/logr v1.4.2 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/google/go-cmp v0.6.0 // indirect - github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect + github.com/google/pprof v0.0.0-20240722153945-304e4f0156b8 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/stretchr/testify v1.9.0 // indirect - golang.org/x/net v0.25.0 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect - golang.org/x/tools v0.21.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect + golang.org/x/tools v0.23.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 4b68188..37cce04 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/pprof v0.0.0-20240528025155-186aa0362fba h1:ql1qNgCyOB7iAEk8JTNM+zJrgIbnyCKX/wdlyPufP5g= -github.com/google/pprof v0.0.0-20240528025155-186aa0362fba/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= +github.com/google/pprof v0.0.0-20240722153945-304e4f0156b8 h1:ssNFCCVmib/GQSzx3uCWyfMgOamLGWuGqlMS77Y1m3Y= +github.com/google/pprof v0.0.0-20240722153945-304e4f0156b8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -18,10 +18,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA= -github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To= -github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk= -github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0= +github.com/onsi/ginkgo/v2 v2.19.1 h1:QXgq3Z8Crl5EL1WBAC98A5sEBHARrAJNzAmMxzLcRF0= +github.com/onsi/ginkgo/v2 v2.19.1/go.mod h1:O3DtEWQkPa/F7fBMgmZQKKsluAy8pd3rEQdrjkPb9zA= +github.com/onsi/gomega v1.34.0 h1:eSSPsPNp6ZpsG8X1OVmOTxig+CblTc4AxpPBykhe2Os= +github.com/onsi/gomega v1.34.0/go.mod h1:MIKI8c+f+QLWk+hxbePD4i0LMJSExPaZOVfkoex4cAo= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= @@ -37,15 +37,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= -golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= +golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/instance.go b/instance.go index 4d16511..9d3d5e4 100644 --- a/instance.go +++ b/instance.go @@ -26,6 +26,7 @@ import ( "os" "os/exec" "os/user" + "path" "path/filepath" "regexp" "strconv" @@ -89,7 +90,7 @@ type Instance struct { executionSysProcAttr *syscall.SysProcAttr // This is used internally to allow execution of Caché code as different users } -// Update will query the the underlying instance and update the Instance fields with its current state. +// Update will query the underlying instance and update the Instance fields with its current state. // It returns any error encountered. func (i *Instance) Update() error { procAttr, err := i.managerSysProc() @@ -260,7 +261,7 @@ func (i *Instance) managerSysProc() (*syscall.SysProcAttr, error) { mgr, _, err := i.DetermineManager() if err != nil { - var pIscErr *ParametersISCNotExistError + var pIscErr *ParametersISCError if errors.As(err, &pIscErr) { log.WithError(pIscErr).Debug("cannot determine manager") return nil, nil @@ -649,6 +650,18 @@ func (i *Instance) ExecuteString(namespace string, code string) (string, error) return i.Execute(namespace, b) } +type ParametersISCError struct { + err error +} + +func (e *ParametersISCError) Error() string { + return e.err.Error() +} + +func (e *ParametersISCError) Unwrap() error { + return e.err +} + type ParametersISCNotExistError struct { dir string err error @@ -668,7 +681,13 @@ func (i *Instance) ReadParametersISC() (ParametersISC, error) { f, err := parameterReader(i.Directory, iscParametersFile) if err != nil { if errors.Is(err, os.ErrNotExist) { - return nil, &ParametersISCNotExistError{dir: i.Directory, err: err} + return nil, &ParametersISCError{&ParametersISCNotExistError{dir: i.Directory, err: err}} + } + + if errors.Is(err, os.ErrPermission) { + return nil, &ParametersISCError{fmt.Errorf("permissions denied %s. Error [%w]", + path.Join(i.Directory, iscParametersFile), + err)} } return nil, err } diff --git a/instance_test.go b/instance_test.go index 5ace185..72b213a 100644 --- a/instance_test.go +++ b/instance_test.go @@ -32,6 +32,7 @@ import ( var _ = Describe("Instance", func() { const ( + instanceName = "INSTTEST" cacheqlist = "INSTTEST^/ensemble/instances/insttest/^2015.2.2.805.0.16216^running, since Fri May 13 22:07:02 2016^cache.cpf^56772^57772^62972^ok^" durableqlist = "INSTTEST^/ensemble/instances/insttest/^2015.2.2.805.0.16216^running, since Fri May 13 22:07:02 2016^cache.cpf^56772^57772^62972^ok^^^^/mgr/config" ensembleqlist = "INSTTEST^/ensemble/instances/insttest/^2015.2.2.805.0.16216^running, since Fri May 13 22:07:02 2016^cache.cpf^56772^57772^62972^ok^Ensemble" @@ -434,6 +435,9 @@ var _ = Describe("Instance", func() { defer can() err = instance.WaitForReady(ctx) }) + AfterEach(func() { + getQlist = qlist + }) It("Does not return an error", func() { Expect(err).NotTo(HaveOccurred()) }) @@ -554,6 +558,16 @@ var _ = Describe("Instance", func() { }) Describe("Update", func() { + BeforeEach(func() { + getQlist = func(instanceName string, _ *syscall.SysProcAttr) (string, error) { + return cacheqlist, nil + } + instance = &Instance{Name: instanceName} + }) + AfterEach(func() { + getQlist = qlist + }) + // To test instance updates when running somewhere that doesn't actually have access to the // parameters.isc file, such as `iscenv` wrapping `csession` or `iris` Context("Valid qlist without parameters.isc", func() { @@ -561,8 +575,21 @@ var _ = Describe("Instance", func() { parameterReader = func(directory string, file string) (io.ReadCloser, error) { return nil, os.ErrNotExist } - instance, err = InstanceFromQList(cacheqlist) - Expect(err).ToNot(HaveOccurred()) + }) + + It("Does not return an error", func() { + err := instance.Update() + Expect(err).NotTo(HaveOccurred()) + }) + }) + + // For when the user doesn't have permissions to read parameters.isc, such as running as a + // non-owner user that is still a valid user in ISC instance + Context("Valid qlist but cannot read parameters.isc", func() { + BeforeEach(func() { + parameterReader = func(directory string, file string) (io.ReadCloser, error) { + return nil, os.ErrPermission + } }) It("Does not return an error", func() {