Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
(5.5) Fix issue with chown when only uid is specified (#1789)
Browse files Browse the repository at this point in the history
* Fix issue with chown when only uid is specified

* Ignore not found authority errors when disabling access
  • Loading branch information
r0mant committed Jun 26, 2020
1 parent 1360a12 commit 7c90d62
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lib/ops/opsservice/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ func newMount(m schema.Volume) storage.Mount {
// chownExpr generates chown expression in the form used by chown command
// based on uid and gid parameters
func chownExpr(uid, gid *int) string {
// When both uid and gid are specified, the syntax is "chown <uid>:<gid> <dir>"
if uid != nil && gid != nil {
return fmt.Sprintf("%v:%v", *uid, *gid)
}
// When only uid is specified, the syntax is "chown <uid> <dir>"
if uid != nil {
return fmt.Sprintf("%v:", *uid)
return fmt.Sprintf("%v", *uid)
}
// When only gid is specified, the syntax is "chown :<gid> <dir>"
return fmt.Sprintf(":%v", *gid)
}
58 changes: 58 additions & 0 deletions lib/ops/opsservice/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2020 Gravitational, Inc.
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.
*/

package opsservice

import (
"github.com/gravitational/gravity/lib/utils"

"gopkg.in/check.v1"
)

type UtilsSuite struct {
}

var _ = check.Suite(&UtilsSuite{})

func (s *UtilsSuite) TestChownExpr(c *check.C) {
tests := []struct {
uid *int
gid *int
result string
comment string
}{
{
uid: utils.IntPtr(1000),
result: "1000",
comment: "only uid is specified",
},
{
gid: utils.IntPtr(2000),
result: ":2000",
comment: "only gid is specified",
},
{
uid: utils.IntPtr(1000),
gid: utils.IntPtr(2000),
result: "1000:2000",
comment: "both uid and gid are specified",
},
}
for _, t := range tests {
c.Assert(chownExpr(t.uid, t.gid), check.Equals, t.result,
check.Commentf(t.comment))
}
}
30 changes: 22 additions & 8 deletions lib/storage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,37 @@ func DisableAccess(backend Backend, name string, delay time.Duration) error {
Type: teleservices.UserCA,
DomainName: name,
}, true)
if err != nil {
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)
}
ca.SetTTL(backend, delay)
if err := backend.UpsertCertAuthority(ca); err != nil {
return trace.Wrap(err)
// User authority may have already been deleted if one of the calls below
// failed and this is being retried.
if trace.IsNotFound(err) {
log.WithField("name", name).Warn("User authority not found.")
}
if ca != nil {
ca.SetTTL(backend, delay)
if err := backend.UpsertCertAuthority(ca); err != nil {
return trace.Wrap(err)
}
}
ca, err = backend.GetCertAuthority(teleservices.CertAuthID{
Type: teleservices.HostCA,
DomainName: name,
}, true)
if err != nil {
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)
}
ca.SetTTL(backend, delay)
if err := backend.UpsertCertAuthority(ca); err != nil {
return trace.Wrap(err)
// Host authority may have already been deleted if one of the calls below
// failed and this is being retried.
if trace.IsNotFound(err) {
log.WithField("name", name).Warn("Host authority not found.")
}
if ca != nil {
ca.SetTTL(backend, delay)
if err := backend.UpsertCertAuthority(ca); err != nil {
return trace.Wrap(err)
}
}
cluster, err := backend.GetTrustedCluster(name)
if err != nil {
Expand Down

0 comments on commit 7c90d62

Please sign in to comment.