2018-04-13 12:31:03 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-04-13 12:31:03 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
package util
|
2018-04-13 12:31:03 +00:00
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
import (
|
2019-09-25 14:45:19 +00:00
|
|
|
"errors"
|
2019-06-01 21:26:42 +00:00
|
|
|
"fmt"
|
2019-06-25 19:29:17 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-06-01 21:26:42 +00:00
|
|
|
)
|
2018-04-13 12:31:03 +00:00
|
|
|
|
|
|
|
const (
|
2019-06-25 19:29:17 +00:00
|
|
|
credUserID = "userID"
|
|
|
|
credUserKey = "userKey"
|
|
|
|
credAdminID = "adminID"
|
|
|
|
credAdminKey = "adminKey"
|
|
|
|
credMonitors = "monitors"
|
|
|
|
tmpKeyFileLocation = "/tmp/csi/keys"
|
|
|
|
tmpKeyFileNamePrefix = "keyfile-"
|
2021-10-24 08:48:28 +00:00
|
|
|
migUserName = "admin"
|
|
|
|
migUserID = "adminId"
|
|
|
|
migUserKey = "key"
|
2018-04-13 12:31:03 +00:00
|
|
|
)
|
|
|
|
|
2020-05-20 07:21:01 +00:00
|
|
|
// Credentials struct represents credentials to access the ceph cluster.
|
2019-06-01 21:26:42 +00:00
|
|
|
type Credentials struct {
|
2019-06-25 19:29:17 +00:00
|
|
|
ID string
|
|
|
|
KeyFile string
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
func storeKey(key string) (string, error) {
|
|
|
|
tmpfile, err := ioutil.TempFile(tmpKeyFileLocation, tmpKeyFileNamePrefix)
|
|
|
|
if err != nil {
|
2020-12-08 14:12:38 +00:00
|
|
|
return "", fmt.Errorf("error creating a temporary keyfile: %w", err)
|
2019-06-25 19:29:17 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
Address security concerns reported by 'gosec'
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>
2019-08-30 10:23:10 +00:00
|
|
|
// don't complain about unhandled error
|
|
|
|
_ = os.Remove(tmpfile.Name())
|
2019-06-25 19:29:17 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if _, err = tmpfile.Write([]byte(key)); err != nil {
|
2020-12-08 14:12:38 +00:00
|
|
|
return "", fmt.Errorf("error writing key to temporary keyfile: %w", err)
|
2019-06-25 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyFile := tmpfile.Name()
|
|
|
|
if keyFile == "" {
|
2020-12-08 14:12:38 +00:00
|
|
|
err = fmt.Errorf("error reading temporary filename for key: %w", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tmpfile.Close(); err != nil {
|
2020-12-08 14:12:38 +00:00
|
|
|
return "", fmt.Errorf("error closing temporary filename: %w", err)
|
2019-06-25 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return keyFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCredentialsFromSecret(idField, keyField string, secrets map[string]string) (*Credentials, error) {
|
2018-04-13 12:31:03 +00:00
|
|
|
var (
|
2019-06-01 21:26:42 +00:00
|
|
|
c = &Credentials{}
|
2018-04-13 12:31:03 +00:00
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
2019-09-25 14:45:19 +00:00
|
|
|
if len(secrets) == 0 {
|
|
|
|
return nil, errors.New("provided secret is empty")
|
|
|
|
}
|
2019-06-01 21:26:42 +00:00
|
|
|
if c.ID, ok = secrets[idField]; !ok {
|
2018-04-13 12:31:03 +00:00
|
|
|
return nil, fmt.Errorf("missing ID field '%s' in secrets", idField)
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
key := secrets[keyField]
|
|
|
|
if key == "" {
|
2018-04-13 12:31:03 +00:00
|
|
|
return nil, fmt.Errorf("missing key field '%s' in secrets", keyField)
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
keyFile, err := storeKey(key)
|
|
|
|
if err == nil {
|
|
|
|
c.KeyFile = keyFile
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, err
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 07:21:01 +00:00
|
|
|
// DeleteCredentials removes the KeyFile.
|
2019-06-25 19:29:17 +00:00
|
|
|
func (cr *Credentials) DeleteCredentials() {
|
Address security concerns reported by 'gosec'
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>
2019-08-30 10:23:10 +00:00
|
|
|
// don't complain about unhandled error
|
|
|
|
_ = os.Remove(cr.KeyFile)
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 07:21:01 +00:00
|
|
|
// NewUserCredentials creates new user credentials from secret.
|
2019-06-25 19:29:17 +00:00
|
|
|
func NewUserCredentials(secrets map[string]string) (*Credentials, error) {
|
|
|
|
return newCredentialsFromSecret(credUserID, credUserKey, secrets)
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:21:01 +00:00
|
|
|
// NewAdminCredentials creates new admin credentials from secret.
|
2019-06-25 19:29:17 +00:00
|
|
|
func NewAdminCredentials(secrets map[string]string) (*Credentials, error) {
|
|
|
|
return newCredentialsFromSecret(credAdminID, credAdminKey, secrets)
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:21:01 +00:00
|
|
|
// GetMonValFromSecret returns monitors from secret.
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetMonValFromSecret(secrets map[string]string) (string, error) {
|
2019-01-18 15:27:48 +00:00
|
|
|
if mons, ok := secrets[credMonitors]; ok {
|
|
|
|
return mons, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-01-18 15:27:48 +00:00
|
|
|
return "", fmt.Errorf("missing %q", credMonitors)
|
|
|
|
}
|
2021-10-24 08:48:28 +00:00
|
|
|
|
|
|
|
// ParseAndSetSecretMapFromMigSecret parse the secretmap from the migration request and return
|
|
|
|
// newsecretmap with the userID and userKey fields set.
|
|
|
|
func ParseAndSetSecretMapFromMigSecret(secretmap map[string]string) (map[string]string, error) {
|
|
|
|
newSecretMap := make(map[string]string)
|
|
|
|
// parse and set userKey
|
|
|
|
if !IsMigrationSecret(secretmap) {
|
|
|
|
return nil, errors.New("passed secret map does not contain user key or it is nil")
|
|
|
|
}
|
|
|
|
newSecretMap[credUserKey] = secretmap[migUserKey]
|
|
|
|
// parse and set the userID
|
|
|
|
newSecretMap[credUserID] = migUserName
|
|
|
|
if secretmap[migUserID] != "" {
|
|
|
|
newSecretMap[credUserID] = secretmap[migUserID]
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSecretMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMigrationSecret validates if the passed in secretmap is a secret
|
|
|
|
// of a migration volume request. The migration secret carry a field
|
|
|
|
// called `key` which is the equivalent of `userKey` which is what we
|
|
|
|
// check here for identifying the secret.
|
|
|
|
func IsMigrationSecret(passedSecretMap map[string]string) bool {
|
|
|
|
// the below 'nil' check is an extra measure as the request validators like
|
|
|
|
// ValidateNodeStageVolumeRequest() already does the nil check, however considering
|
|
|
|
// this function can be called independently with a map of secret values
|
|
|
|
// it is good to have this check in place, also it gives clear error about this
|
|
|
|
// was hit on migration request compared to general one.
|
|
|
|
return len(passedSecretMap) != 0 && passedSecretMap[migUserKey] != ""
|
|
|
|
}
|