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

(5.5) Fix issue with chown when only uid is specified #1789

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -250,23 +250,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