mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-10 00:10:20 +00:00
dd668e59f1
gosec reports several issues, none of them looks very critical. With this change the following concerns have been addressed: [pkg/cephfs/nodeserver.go:229] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.Chmod(targetPath, 0777) [pkg/cephfs/util.go:39] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM) > exec.Command(program, args...) [pkg/rbd/nodeserver.go:156] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.Chmod(stagingTargetPath, 0777) [pkg/rbd/nodeserver.go:205] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750) [pkg/rbd/rbd_util.go:797] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM) > ioutil.ReadFile(fPath) [pkg/util/cephcmds.go:35] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM) > exec.Command(program, args...) [pkg/util/credentials.go:47] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW) > os.Remove(tmpfile.Name()) [pkg/util/credentials.go:92] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW) > os.Remove(cr.KeyFile) [pkg/util/pidlimit.go:74] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM) > os.Open(pidsMax) URL: https://github.com/securego/gosec Signed-off-by: Niels de Vos <ndevos@redhat.com>
123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
/*
|
|
Copyright 2018 The Ceph-CSI Authors.
|
|
|
|
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 util
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
credUserID = "userID"
|
|
credUserKey = "userKey"
|
|
credAdminID = "adminID"
|
|
credAdminKey = "adminKey"
|
|
credMonitors = "monitors"
|
|
tmpKeyFileLocation = "/tmp/csi/keys"
|
|
tmpKeyFileNamePrefix = "keyfile-"
|
|
)
|
|
|
|
type Credentials struct {
|
|
ID string
|
|
KeyFile string
|
|
}
|
|
|
|
func storeKey(key string) (string, error) {
|
|
tmpfile, err := ioutil.TempFile(tmpKeyFileLocation, tmpKeyFileNamePrefix)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error creating a temporary keyfile (%s)", err)
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
// don't complain about unhandled error
|
|
_ = os.Remove(tmpfile.Name())
|
|
}
|
|
}()
|
|
|
|
if _, err = tmpfile.Write([]byte(key)); err != nil {
|
|
return "", fmt.Errorf("error writing key to temporary keyfile (%s)", err)
|
|
}
|
|
|
|
keyFile := tmpfile.Name()
|
|
if keyFile == "" {
|
|
err = fmt.Errorf("error reading temporary filename for key (%s)", err)
|
|
return "", err
|
|
}
|
|
|
|
if err = tmpfile.Close(); err != nil {
|
|
return "", fmt.Errorf("error closing temporary filename (%s)", err)
|
|
}
|
|
|
|
return keyFile, nil
|
|
}
|
|
|
|
func newCredentialsFromSecret(idField, keyField string, secrets map[string]string) (*Credentials, error) {
|
|
var (
|
|
c = &Credentials{}
|
|
ok bool
|
|
)
|
|
|
|
if c.ID, ok = secrets[idField]; !ok {
|
|
return nil, fmt.Errorf("missing ID field '%s' in secrets", idField)
|
|
}
|
|
|
|
key := secrets[keyField]
|
|
if key == "" {
|
|
return nil, fmt.Errorf("missing key field '%s' in secrets", keyField)
|
|
}
|
|
|
|
keyFile, err := storeKey(key)
|
|
if err == nil {
|
|
c.KeyFile = keyFile
|
|
}
|
|
|
|
return c, err
|
|
}
|
|
|
|
func (cr *Credentials) DeleteCredentials() {
|
|
// don't complain about unhandled error
|
|
_ = os.Remove(cr.KeyFile)
|
|
}
|
|
|
|
func NewUserCredentials(secrets map[string]string) (*Credentials, error) {
|
|
return newCredentialsFromSecret(credUserID, credUserKey, secrets)
|
|
}
|
|
|
|
func NewAdminCredentials(secrets map[string]string) (*Credentials, error) {
|
|
return newCredentialsFromSecret(credAdminID, credAdminKey, secrets)
|
|
}
|
|
|
|
func NewCredentials(id, key string) (*Credentials, error) {
|
|
var c = &Credentials{}
|
|
|
|
c.ID = id
|
|
keyFile, err := storeKey(key)
|
|
if err == nil {
|
|
c.KeyFile = keyFile
|
|
}
|
|
|
|
return c, err
|
|
}
|
|
|
|
func GetMonValFromSecret(secrets map[string]string) (string, error) {
|
|
if mons, ok := secrets[credMonitors]; ok {
|
|
return mons, nil
|
|
}
|
|
return "", fmt.Errorf("missing %q", credMonitors)
|
|
}
|