|
| 1 | +// +build !windows |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * The OpenSearch Contributors require contributions made to |
| 7 | + * this file be licensed under the Apache-2.0 license or a |
| 8 | + * compatible open source license. |
| 9 | + * |
| 10 | + * Modifications Copyright OpenSearch Contributors. See |
| 11 | + * GitHub history for details. |
| 12 | + */ |
| 13 | + |
| 14 | +package commands |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + FolderPermission = 0700 // only owner can read, write and execute |
| 24 | + FilePermission = 0600 // only owner can read and write |
| 25 | +) |
| 26 | + |
| 27 | +// createDefaultConfigFolderIfNotExists creates default config file along with folder if |
| 28 | +// it doesn't exists |
| 29 | +func createDefaultConfigFileIfNotExists() error { |
| 30 | + defaultFilePath := GetDefaultConfigFilePath() |
| 31 | + if isExists(defaultFilePath) { |
| 32 | + return nil |
| 33 | + } |
| 34 | + folderPath := filepath.Dir(defaultFilePath) |
| 35 | + if !isExists(folderPath) { |
| 36 | + err := os.Mkdir(folderPath, FolderPermission) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + } |
| 41 | + f, err := os.Create(defaultFilePath) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + if err = f.Chmod(FilePermission); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + return f.Close() |
| 49 | +} |
| 50 | + |
| 51 | +func checkConfigFilePermission(configFilePath string) error { |
| 52 | + //check for config file permission |
| 53 | + info, err := os.Stat(configFilePath) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get config file info due to: %w", err) |
| 56 | + } |
| 57 | + mode := info.Mode().Perm() |
| 58 | + |
| 59 | + if mode != FilePermission { |
| 60 | + return fmt.Errorf("permissions %o for '%s' are too open. It is required that your config file is NOT accessible by others", mode, configFilePath) |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
0 commit comments