|
| 1 | +/* |
| 2 | +Copyright © 2019 Vineeth Pothulapati <[email protected]> |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +package cmd |
| 24 | + |
| 25 | +import ( |
| 26 | + "encoding/json" |
| 27 | + "fmt" |
| 28 | + "github.com/spf13/cobra" |
| 29 | + PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/apimachinery/pkg/types" |
| 32 | + "log" |
| 33 | + "strings" |
| 34 | +) |
| 35 | + |
| 36 | +var allowedPrivileges = []string{"SUPERUSER", "REPLICATION", "INHERIT", "LOGIN", "NOLOGIN", "CREATEROLE", "CREATEDB", "BYPASSURL"} |
| 37 | + |
| 38 | +// addUserCmd represents the addUser command |
| 39 | +var addUserCmd = &cobra.Command{ |
| 40 | + Use: "add-user", |
| 41 | + Short: "Adds a user to the postgres cluster with given privileges", |
| 42 | + Long: `Adds a user to the postgres cluster. You can add privileges as well with -p flag.`, |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + clusterName, _ := cmd.Flags().GetString("cluster") |
| 45 | + privileges, _ := cmd.Flags().GetString("privileges") |
| 46 | + |
| 47 | + if len(args) > 0 { |
| 48 | + user := args[0] |
| 49 | + var permissions []string |
| 50 | + var perms []string |
| 51 | + |
| 52 | + if privileges != "" { |
| 53 | + parsedRoles := strings.Replace(privileges, ",", " ", -1) |
| 54 | + parsedRoles = strings.ToUpper(parsedRoles) |
| 55 | + permissions = strings.Fields(parsedRoles) |
| 56 | + var invalidPerms []string |
| 57 | + |
| 58 | + for _, userPrivilege := range permissions { |
| 59 | + validPerm := false |
| 60 | + for _, privilege := range allowedPrivileges { |
| 61 | + if privilege == userPrivilege { |
| 62 | + perms = append(perms, userPrivilege) |
| 63 | + validPerm = true |
| 64 | + } |
| 65 | + } |
| 66 | + if !validPerm { |
| 67 | + invalidPerms = append(invalidPerms, userPrivilege) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if len(invalidPerms) > 0 { |
| 72 | + fmt.Printf("Invalid privileges %s\n", invalidPerms) |
| 73 | + return |
| 74 | + } |
| 75 | + } |
| 76 | + addUser(user, clusterName, perms) |
| 77 | + } |
| 78 | + }, |
| 79 | + Example: ` |
| 80 | +kubectl pg add-user user01 -p login,createdb -c cluster01 |
| 81 | +`, |
| 82 | +} |
| 83 | + |
| 84 | +// add user to the cluster with provided permissions |
| 85 | +func addUser(user string, clusterName string, permissions []string) { |
| 86 | + config := getConfig() |
| 87 | + postgresConfig, err := PostgresqlLister.NewForConfig(config) |
| 88 | + if err != nil { |
| 89 | + log.Fatal(err) |
| 90 | + } |
| 91 | + |
| 92 | + namespace := getCurrentNamespace() |
| 93 | + postgresql, err := postgresConfig.Postgresqls(namespace).Get(clusterName, metav1.GetOptions{}) |
| 94 | + if err != nil { |
| 95 | + log.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + setUsers := make(map[string]bool) |
| 99 | + for _, k := range permissions { |
| 100 | + setUsers[k] = true |
| 101 | + } |
| 102 | + |
| 103 | + if existingRoles, key := postgresql.Spec.Users[user]; key { |
| 104 | + for _, k := range existingRoles { |
| 105 | + setUsers[k] = true |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + Privileges := []string{} |
| 110 | + for keys, values := range setUsers { |
| 111 | + if values { |
| 112 | + Privileges = append(Privileges, keys) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + patch := applyUserPatch(user, Privileges) |
| 117 | + updatedPostgresql, err := postgresConfig.Postgresqls(namespace).Patch(postgresql.Name, types.MergePatchType, patch, "") |
| 118 | + if err != nil { |
| 119 | + log.Fatal(err) |
| 120 | + } |
| 121 | + |
| 122 | + if updatedPostgresql.ResourceVersion != postgresql.ResourceVersion { |
| 123 | + fmt.Printf("postgresql %s is updated with new user %s and with privileges %s.\n", updatedPostgresql.Name, user, permissions) |
| 124 | + } else { |
| 125 | + fmt.Printf("postgresql %s is unchanged.\n", updatedPostgresql.Name) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func applyUserPatch(user string, value []string) []byte { |
| 130 | + ins := map[string]map[string]map[string][]string{"spec": {"users": {user: value}}} |
| 131 | + patchInstances, err := json.Marshal(ins) |
| 132 | + if err != nil { |
| 133 | + log.Fatal(err, "unable to parse number of instances json") |
| 134 | + } |
| 135 | + return patchInstances |
| 136 | +} |
| 137 | + |
| 138 | +func init() { |
| 139 | + addUserCmd.Flags().StringP("cluster", "c", "", "add user to the provided cluster.") |
| 140 | + addUserCmd.Flags().StringP("privileges", "p", "", "add privileges separated by commas without spaces") |
| 141 | + rootCmd.AddCommand(addUserCmd) |
| 142 | +} |
0 commit comments