YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/pt_create_role.h
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//
15
// Tree node definitions for CREATE ROLE statement.
16
//--------------------------------------------------------------------------------------------------
17
18
#ifndef YB_YQL_CQL_QL_PTREE_PT_CREATE_ROLE_H
19
#define YB_YQL_CQL_QL_PTREE_PT_CREATE_ROLE_H
20
21
#include "yb/yql/cql/ql/ptree/tree_node.h"
22
#include "yb/yql/cql/ql/ptree/pt_name.h"
23
#include "yb/util/crypt.h"
24
25
namespace yb {
26
namespace ql {
27
using yb::util::kBcryptHashSize;
28
//--------------------------------------------------------------------------------------------------
29
// Roles.
30
31
enum class PTRoleOptionType {
32
  kLogin,
33
  kPassword,
34
  kSuperuser,
35
};
36
37
class PTRoleOption : public TreeNode {
38
 public:
39
  //------------------------------------------------------------------------------------------------
40
  // Public types.
41
  typedef MCSharedPtr<PTRoleOption> SharedPtr;
42
  typedef MCSharedPtr<const PTRoleOption> SharedPtrConst;
43
44
  //------------------------------------------------------------------------------------------------
45
  // Constructor and destructor.
46
  explicit PTRoleOption(MemoryContext* memctx = nullptr, YBLocationPtr loc = nullptr)
47
2.19k
      : TreeNode(memctx, loc) {
48
2.19k
  }
49
2.19k
  virtual ~PTRoleOption() {
50
2.19k
  }
51
52
  // Node type.
53
0
  virtual TreeNodeOpcode opcode() const override {
54
0
    return TreeNodeOpcode::kPTRoleOption;
55
0
  }
56
57
  virtual PTRoleOptionType option_type() = 0;
58
};
59
60
using PTRoleOptionListNode = TreeListNode<PTRoleOption>;
61
62
class PTRolePassword : public PTRoleOption {
63
 public:
64
  //------------------------------------------------------------------------------------------------
65
  // Public types.
66
  typedef MCSharedPtr<PTRolePassword> SharedPtr;
67
  typedef MCSharedPtr<const PTRolePassword> SharedPtrConst;
68
69
  //------------------------------------------------------------------------------------------------
70
  // Constructor and destructor.
71
72
  PTRolePassword(MemoryContext* memctx,
73
                 YBLocationPtr loc,
74
                 const MCSharedPtr<MCString>& password);
75
76
  virtual ~PTRolePassword();
77
78
712
  virtual PTRoleOptionType option_type() override {
79
712
    return PTRoleOptionType::kPassword;
80
712
  }
81
82
  template<typename... TypeArgs>
83
712
  inline static PTRolePassword::SharedPtr MakeShared(MemoryContext* memctx, TypeArgs&&... args) {
84
712
    return MCMakeShared<PTRolePassword>(memctx, std::forward<TypeArgs>(args)...);
85
712
  }
86
87
  // Node semantics analysis.
88
712
  virtual CHECKED_STATUS Analyze(SemContext* sem_context) override {
89
712
    return Status::OK();
90
712
  }
91
92
712
  const char* password() const {
93
712
    return password_->c_str();
94
712
  }
95
96
 private:
97
  const MCSharedPtr<MCString> password_;
98
99
};
100
101
class PTRoleLogin : public PTRoleOption {
102
 public:
103
  //------------------------------------------------------------------------------------------------
104
  // Public types.
105
  typedef MCSharedPtr<PTRoleLogin> SharedPtr;
106
  typedef MCSharedPtr<const PTRoleLogin> SharedPtrConst;
107
108
  //------------------------------------------------------------------------------------------------
109
  // Constructor and destructor.
110
111
  PTRoleLogin(MemoryContext *memctx,
112
              YBLocationPtr loc,
113
              bool login);
114
115
  virtual ~PTRoleLogin();
116
117
740
  virtual PTRoleOptionType option_type() override {
118
740
    return PTRoleOptionType::kLogin;
119
740
  }
120
121
  template<typename... TypeArgs>
122
740
  inline static PTRoleLogin::SharedPtr MakeShared(MemoryContext* memctx, TypeArgs&&... args) {
123
740
    return MCMakeShared<PTRoleLogin>(memctx, std::forward<TypeArgs>(args)...);
124
740
  }
125
126
  // Node semantics analysis.
127
740
  virtual CHECKED_STATUS Analyze(SemContext* sem_context) override {
128
740
    return Status::OK();
129
740
  }
130
131
740
  bool login() const {
132
740
    return login_;
133
740
  }
134
135
 private:
136
  const bool login_;
137
};
138
139
class PTRoleSuperuser : public PTRoleOption {
140
 public:
141
  //------------------------------------------------------------------------------------------------
142
  // Public types.
143
  typedef MCSharedPtr<PTRoleSuperuser> SharedPtr;
144
  typedef MCSharedPtr<const PTRoleSuperuser> SharedPtrConst;
145
146
  //------------------------------------------------------------------------------------------------
147
  // Constructor and destructor.
148
149
  PTRoleSuperuser(MemoryContext *memctx,
150
                  YBLocationPtr loc,
151
                  bool superuser);
152
153
  virtual ~PTRoleSuperuser();
154
155
741
  virtual PTRoleOptionType option_type() override {
156
741
    return PTRoleOptionType::kSuperuser;
157
741
  }
158
159
  template<typename... TypeArgs>
160
741
  inline static PTRoleSuperuser::SharedPtr MakeShared(MemoryContext* memctx, TypeArgs&&... args) {
161
741
    return MCMakeShared<PTRoleSuperuser>(memctx, std::forward<TypeArgs>(args)...);
162
741
  }
163
164
  // Node semantics analysis.
165
741
  virtual CHECKED_STATUS Analyze(SemContext* sem_context) override {
166
741
    return Status::OK();
167
741
  }
168
169
741
  bool superuser() const {
170
741
    return superuser_;
171
741
  }
172
173
 private:
174
  const bool superuser_;
175
};
176
177
//--------------------------------------------------------------------------------------------------
178
// CREATE ROLE statement.
179
180
class PTCreateRole : public TreeNode {
181
 public:
182
  //------------------------------------------------------------------------------------------------
183
  // Public types.
184
  typedef MCSharedPtr<PTCreateRole> SharedPtr;
185
  typedef MCSharedPtr<const PTCreateRole> SharedPtrConst;
186
187
  //------------------------------------------------------------------------------------------------
188
  // Constructor and destructor.
189
  PTCreateRole(MemoryContext* memctx,
190
               YBLocationPtr loc,
191
               const MCSharedPtr<MCString>& name,
192
               const PTRoleOptionListNode::SharedPtr& roleOptions,
193
               bool create_if_not_exists);
194
  virtual ~PTCreateRole();
195
196
  // Node type.
197
3.78k
  virtual TreeNodeOpcode opcode() const override {
198
3.78k
    return TreeNodeOpcode::kPTCreateRole;
199
3.78k
  }
200
201
  // Support for shared_ptr.
202
  template<typename... TypeArgs>
203
757
  inline static PTCreateRole::SharedPtr MakeShared(MemoryContext* memctx, TypeArgs&&... args) {
204
757
    return MCMakeShared<PTCreateRole>(memctx, std::forward<TypeArgs>(args)...);
205
757
  }
206
207
  // Node semantics analysis.
208
  virtual CHECKED_STATUS Analyze(SemContext* sem_context) override;
209
  void PrintSemanticAnalysisResult(SemContext* sem_context);
210
211
  // Role name.
212
757
  const char* role_name() const {
213
757
    return name_->c_str();
214
757
  }
215
216
757
  std::string salted_hash() const {
217
    // Empty salted hash denotes no password. salted_hash can contain null characters.
218
757
    return (salted_hash_ != nullptr) ?  
std::string(salted_hash_->c_str(), kBcryptHashSize)703
:
""54
;
219
757
  }
220
221
757
  bool superuser() const {
222
757
    return superuser_;
223
757
  }
224
225
757
  bool login() const {
226
757
    return login_;
227
757
  }
228
229
6
  bool create_if_not_exists() const {
230
6
    return create_if_not_exists_;
231
6
  }
232
233
 private:
234
235
  const MCSharedPtr<MCString>  name_;
236
  PTRoleOptionListNode::SharedPtr roleOptions_;
237
  MCSharedPtr<MCString> salted_hash_ = nullptr;
238
  bool login_ = false;
239
  bool superuser_ = false;
240
  const bool create_if_not_exists_;
241
242
};
243
244
}  // namespace ql
245
}  // namespace yb
246
247
#endif // YB_YQL_CQL_QL_PTREE_PT_CREATE_ROLE_H