|
| 1 | +/* |
| 2 | +Copyright 2021. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/jackc/pgx/v4" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. |
| 25 | + |
| 26 | +// PostgresGrantSpec defines the desired state of PostgresGrant |
| 27 | +type PostgresGrantSpec struct { |
| 28 | + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster |
| 29 | + // Important: Run "make" to regenerate code after modifying this file |
| 30 | + |
| 31 | + // PostgresRef is a reference to the PostgreSQL server to configure |
| 32 | + PostgresRef PostgresRef `json:"postgresRef"` |
| 33 | + |
| 34 | + // Role to grant privileges to |
| 35 | + Role string `json:"role"` |
| 36 | + |
| 37 | + // Tables is the list of tables to grant privileges for |
| 38 | + Tables []PostgresIdentifier `json:"tables,omitempty"` |
| 39 | + |
| 40 | + // TablePrivileges is the list of privileges to grant |
| 41 | + // Must be one of: SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, ALL |
| 42 | + TablePrivileges []string `json:"tablePrivileges,omitempty"` |
| 43 | + |
| 44 | + // Sequences is the list of sequences to grant privileges for |
| 45 | + Sequences []PostgresIdentifier `json:"sequences,omitempty"` |
| 46 | + |
| 47 | + // SequencePrivileges is the list of privileges to grant |
| 48 | + // Must be one of: USAGE, SELECT, UPDATE, ALL |
| 49 | + SequencePrivileges []string `json:"sequencePrivileges,omitempty"` |
| 50 | + |
| 51 | + // Databases is the list of databases to grant privileges for |
| 52 | + Databases []string `json:"databases,omitempty"` |
| 53 | + |
| 54 | + // DatabasePrivileges is the list of privileges to grant |
| 55 | + // Must be one of: CREATE, CONNECT, TEMPORARY, TEMP, ALL |
| 56 | + DatabasePrivileges []string `json:"databasePrivileges,omitempty"` |
| 57 | + |
| 58 | + // Schemas is the list of schemas to grant privileges for |
| 59 | + Schemas []string `json:"schemas,omitempty"` |
| 60 | + |
| 61 | + // SchemaPrivileges is the list of privileges to grant |
| 62 | + // Must be one of: CREATE, USAGE, ALL |
| 63 | + SchemaPrivileges []string `json:"schemaPrivileges,omitempty"` |
| 64 | + |
| 65 | + // Functions is the list of functions to grant privileges for |
| 66 | + Functions []PostgresIdentifier `json:"functions,omitempty"` |
| 67 | + |
| 68 | + // FunctionPrivileges is the list of privileges to grant |
| 69 | + // Must be one of: EXECUTE, ALL |
| 70 | + FunctionPrivileges []string `json:"functionPrivileges,omitempty"` |
| 71 | +} |
| 72 | + |
| 73 | +// PostgresGrantStatus defines the observed state of PostgresGrant |
| 74 | +type PostgresGrantStatus struct { |
| 75 | + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster |
| 76 | + // Important: Run "make" to regenerate code after modifying this file |
| 77 | +} |
| 78 | + |
| 79 | +//+kubebuilder:object:root=true |
| 80 | +//+kubebuilder:subresource:status |
| 81 | + |
| 82 | +// PostgresGrant is the Schema for the postgresgrants API |
| 83 | +type PostgresGrant struct { |
| 84 | + metav1.TypeMeta `json:",inline"` |
| 85 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 86 | + |
| 87 | + Spec PostgresGrantSpec `json:"spec,omitempty"` |
| 88 | + Status PostgresGrantStatus `json:"status,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// PrivilegesForType returns the list of privileges defined for a given object |
| 92 | +// type within the grant. If the object type is invalid, nil is returned. |
| 93 | +func (g *PostgresGrant) PrivilegesForType(o ObjectType) []string { |
| 94 | + switch o { |
| 95 | + case ObjectTypeTable: |
| 96 | + return g.Spec.TablePrivileges |
| 97 | + case ObjectTypeSequence: |
| 98 | + return g.Spec.SequencePrivileges |
| 99 | + case ObjectTypeFunction: |
| 100 | + return g.Spec.FunctionPrivileges |
| 101 | + case ObjectTypeSchema: |
| 102 | + return g.Spec.SchemaPrivileges |
| 103 | + case ObjectTypeDatabase: |
| 104 | + return g.Spec.DatabasePrivileges |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// IdentifiersForType returns the list of identifiers defined for a given object |
| 111 | +// type within the grant. If the object type is invalid, nil is returned. |
| 112 | +func (g *PostgresGrant) IdentifiersForType(o ObjectType) []pgx.Identifier { |
| 113 | + var identifiers []pgx.Identifier |
| 114 | + switch o { |
| 115 | + case ObjectTypeTable: |
| 116 | + for _, table := range g.Spec.Tables { |
| 117 | + identifiers = append(identifiers, pgx.Identifier{table.Schema, table.Name}) |
| 118 | + } |
| 119 | + case ObjectTypeSequence: |
| 120 | + for _, sequence := range g.Spec.Sequences { |
| 121 | + identifiers = append(identifiers, pgx.Identifier{sequence.Schema, sequence.Name}) |
| 122 | + } |
| 123 | + case ObjectTypeFunction: |
| 124 | + for _, function := range g.Spec.Functions { |
| 125 | + identifiers = append(identifiers, pgx.Identifier{function.Schema, function.Name}) |
| 126 | + } |
| 127 | + case ObjectTypeSchema: |
| 128 | + for _, schema := range g.Spec.Schemas { |
| 129 | + identifiers = append(identifiers, pgx.Identifier{schema}) |
| 130 | + } |
| 131 | + case ObjectTypeDatabase: |
| 132 | + for _, database := range g.Spec.Databases { |
| 133 | + identifiers = append(identifiers, pgx.Identifier{database}) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return identifiers |
| 138 | +} |
| 139 | + |
| 140 | +//+kubebuilder:object:root=true |
| 141 | + |
| 142 | +// PostgresGrantList contains a list of PostgresGrant |
| 143 | +type PostgresGrantList struct { |
| 144 | + metav1.TypeMeta `json:",inline"` |
| 145 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 146 | + Items []PostgresGrant `json:"items"` |
| 147 | +} |
| 148 | + |
| 149 | +func init() { |
| 150 | + SchemeBuilder.Register(&PostgresGrant{}, &PostgresGrantList{}) |
| 151 | +} |
0 commit comments