]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/Config.pm
api: storage/config: use extract_sensitive_params from tools
[pve-storage.git] / PVE / API2 / Storage / Config.pm
CommitLineData
b6cf0a66
DM
1package PVE::API2::Storage::Config;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
acc9c3ac 7use PVE::Tools qw(extract_param extract_sensitive_params);
b6cf0a66
DM
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
9use PVE::Storage;
1dc01b9f 10use PVE::Storage::Plugin;
304344ce 11use PVE::Storage::LVMPlugin;
a9db2ca8 12use PVE::Storage::CIFSPlugin;
b6cf0a66
DM
13use HTTP::Status qw(:constants);
14use Storable qw(dclone);
15use PVE::JSONSchema qw(get_standard_option);
5f642f73 16use PVE::RPCEnvironment;
b6cf0a66
DM
17
18use PVE::RESTHandler;
19
20use base qw(PVE::RESTHandler);
21
22my @ctypes = qw(images vztmpl iso backup);
23
1dc01b9f 24my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
b6cf0a66
DM
25
26my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
28
1dc01b9f 29 my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid));
b6cf0a66 30 $scfg->{storage} = $storeid;
b6cf0a66 31 $scfg->{digest} = $cfg->{digest};
1dc01b9f 32 $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
b6cf0a66
DM
33
34 if ($scfg->{nodes}) {
1dc01b9f 35 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
b6cf0a66
DM
36 }
37
38 return $scfg;
39};
40
41__PACKAGE__->register_method ({
37ab64f3 42 name => 'index',
b6cf0a66
DM
43 path => '',
44 method => 'GET',
45 description => "Storage index.",
37ab64f3 46 permissions => {
5f642f73
DM
47 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
48 user => 'all',
49 },
b6cf0a66 50 parameters => {
37ab64f3 51 additionalProperties => 0,
b6cf0a66 52 properties => {
37ab64f3 53 type => {
b6cf0a66 54 description => "Only list storage of specific type",
37ab64f3 55 type => 'string',
b6cf0a66
DM
56 enum => $storage_type_enum,
57 optional => 1,
58 },
b6cf0a66
DM
59 },
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => { storage => { type => 'string'} },
66 },
67 links => [ { rel => 'child', href => "{storage}" } ],
68 },
69 code => sub {
70 my ($param) = @_;
71
5f642f73
DM
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
83d7192f 75 my $cfg = PVE::Storage::config();
b6cf0a66 76
5f642f73 77 my @sids = PVE::Storage::storage_ids($cfg);
b6cf0a66
DM
78
79 my $res = [];
80 foreach my $storeid (@sids) {
5f642f73
DM
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83
b6cf0a66
DM
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type} && $param->{type} ne $scfg->{type};
86 push @$res, $scfg;
87 }
88
89 return $res;
90 }});
91
92__PACKAGE__->register_method ({
37ab64f3 93 name => 'read',
b6cf0a66
DM
94 path => '{storage}',
95 method => 'GET',
96 description => "Read storage configuration.",
37ab64f3 97 permissions => {
5f642f73
DM
98 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 },
b6cf0a66 100 parameters => {
37ab64f3 101 additionalProperties => 0,
b6cf0a66
DM
102 properties => {
103 storage => get_standard_option('pve-storage-id'),
104 },
105 },
8b3d5c1f 106 returns => { type => 'object' },
b6cf0a66
DM
107 code => sub {
108 my ($param) = @_;
109
83d7192f 110 my $cfg = PVE::Storage::config();
b6cf0a66
DM
111
112 return &$api_storage_config($cfg, $param->{storage});
113 }});
114
acc9c3ac 115my $sensitive_params = [qw(password encryption-key)];
72385de9 116
b6cf0a66
DM
117__PACKAGE__->register_method ({
118 name => 'create',
119 protected => 1,
37ab64f3 120 path => '',
b6cf0a66
DM
121 method => 'POST',
122 description => "Create a new storage.",
37ab64f3 123 permissions => {
5f642f73
DM
124 check => ['perm', '/storage', ['Datastore.Allocate']],
125 },
1dc01b9f 126 parameters => PVE::Storage::Plugin->createSchema(),
cd69cedf
TL
127 returns => {
128 type => 'object',
129 properties => {
130 storage => {
131 description => "The ID of the created storage.",
132 type => 'string',
133 },
134 type => {
135 description => "The type of the created storage.",
136 type => 'string',
137 enum => $storage_type_enum,
138 },
139 config => {
140 description => "Partial, possible server generated, configuration properties.",
141 type => 'object',
142 optional => 1,
143 additionalProperties => 1,
144 properties => {
145 'encryption-key' => {
146 description => "The, possible auto-generated, encryption-key.",
147 optional => 1,
148 type => 'string',
149 },
150 },
151 },
152 },
153 },
b6cf0a66
DM
154 code => sub {
155 my ($param) = @_;
156
1dc01b9f
DM
157 my $type = extract_param($param, 'type');
158 my $storeid = extract_param($param, 'storage');
b6cf0a66 159
a4a9405d
WL
160 # revent an empty nodelist.
161 # fix me in section config create never need an empty entity.
162 delete $param->{nodes} if !$param->{nodes};
163
acc9c3ac 164 my $sensitive = extract_sensitive_params($param, $sensitive_params, []);
a4a9405d 165
1dc01b9f
DM
166 my $plugin = PVE::Storage::Plugin->lookup($type);
167 my $opts = $plugin->check_config($storeid, $param, 1, 1);
b6cf0a66 168
cd69cedf
TL
169 my $returned_config;
170 PVE::Storage::lock_storage_config(sub {
8ff8e277 171 my $cfg = PVE::Storage::config();
b6cf0a66 172
8ff8e277
TL
173 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
174 die "storage ID '$storeid' already defined\n";
175 }
b6cf0a66 176
8ff8e277 177 $cfg->{ids}->{$storeid} = $opts;
b6cf0a66 178
cd69cedf 179 $returned_config = $plugin->on_add_hook($storeid, $opts, %$sensitive);
8ff8e277
TL
180
181 eval {
182 # try to activate if enabled on local node,
183 # we only do this to detect errors/problems sooner
184 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
185 PVE::Storage::activate_storage($cfg, $storeid);
b6cf0a66 186 }
8ff8e277
TL
187 };
188 if (my $err = $@) {
189 eval { $plugin->on_delete_hook($storeid, $opts) };
190 warn "$@\n" if $@;
191 die $err;
192 }
b6cf0a66 193
8ff8e277 194 PVE::Storage::write_config($cfg);
37ab64f3 195
8ff8e277 196 }, "create storage failed");
b6cf0a66 197
cd69cedf
TL
198 my $res = {
199 storage => $storeid,
200 type => $type,
201 };
202 $res->{config} = $returned_config if $returned_config;
203 return $res;
b6cf0a66
DM
204 }});
205
206__PACKAGE__->register_method ({
207 name => 'update',
208 protected => 1,
209 path => '{storage}',
210 method => 'PUT',
211 description => "Update storage configuration.",
37ab64f3 212 permissions => {
5f642f73
DM
213 check => ['perm', '/storage', ['Datastore.Allocate']],
214 },
1dc01b9f 215 parameters => PVE::Storage::Plugin->updateSchema(),
cd69cedf
TL
216 returns => {
217 type => 'object',
218 properties => {
219 storage => {
220 description => "The ID of the created storage.",
221 type => 'string',
222 },
223 type => {
224 description => "The type of the created storage.",
225 type => 'string',
226 enum => $storage_type_enum,
227 },
228 config => {
229 description => "Partial, possible server generated, configuration properties.",
230 type => 'object',
231 optional => 1,
232 additionalProperties => 1,
233 properties => {
234 'encryption-key' => {
235 description => "The, possible auto-generated, encryption-key.",
236 optional => 1,
237 type => 'string',
238 },
239 },
240 },
241 },
242 },
b6cf0a66
DM
243 code => sub {
244 my ($param) = @_;
245
1dc01b9f
DM
246 my $storeid = extract_param($param, 'storage');
247 my $digest = extract_param($param, 'digest');
4273e3ac 248 my $delete = extract_param($param, 'delete');
cd69cedf 249 my $type;
b6cf0a66 250
72385de9
WB
251 if ($delete) {
252 $delete = [ PVE::Tools::split_list($delete) ];
253 }
254
cd69cedf 255 my $returned_config;
37ab64f3 256 PVE::Storage::lock_storage_config(sub {
83d7192f 257 my $cfg = PVE::Storage::config();
b6cf0a66 258
1dc01b9f 259 PVE::SectionConfig::assert_if_modified($cfg, $digest);
b6cf0a66 260
1dc01b9f 261 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
cd69cedf 262 $type = $scfg->{type};
0ff4cfea 263
acc9c3ac 264 my $sensitive = extract_sensitive_params($param, $sensitive_params, $delete);
b6cf0a66 265
0ff4cfea 266 my $plugin = PVE::Storage::Plugin->lookup($type);
1dc01b9f 267 my $opts = $plugin->check_config($storeid, $param, 0, 1);
b6cf0a66 268
4273e3ac 269 if ($delete) {
0ff4cfea 270 my $options = $plugin->private()->{options}->{$type};
72385de9 271 foreach my $k (@$delete) {
4273e3ac
TL
272 my $d = $options->{$k} || die "no such option '$k'\n";
273 die "unable to delete required option '$k'\n" if !$d->{optional};
274 die "unable to delete fixed option '$k'\n" if $d->{fixed};
275 die "cannot set and delete property '$k' at the same time!\n"
276 if defined($opts->{$k});
277
278 delete $scfg->{$k};
279 }
280 }
281
cd69cedf 282 $returned_config = $plugin->on_update_hook($storeid, $opts, %$sensitive);
0ff4cfea 283
91f42b33 284 for my $k (keys %$opts) {
b6cf0a66
DM
285 $scfg->{$k} = $opts->{$k};
286 }
287
83d7192f 288 PVE::Storage::write_config($cfg);
b6cf0a66 289
37ab64f3 290 }, "update storage failed");
b6cf0a66 291
cd69cedf
TL
292 my $res = {
293 storage => $storeid,
294 type => $type,
295 };
296 $res->{config} = $returned_config if $returned_config;
297 return $res;
b6cf0a66
DM
298 }});
299
300__PACKAGE__->register_method ({
301 name => 'delete',
302 protected => 1,
303 path => '{storage}', # /storage/config/{storage}
304 method => 'DELETE',
305 description => "Delete storage configuration.",
37ab64f3 306 permissions => {
5f642f73
DM
307 check => ['perm', '/storage', ['Datastore.Allocate']],
308 },
b6cf0a66 309 parameters => {
37ab64f3
TL
310 additionalProperties => 0,
311 properties => {
f3bd890d 312 storage => get_standard_option('pve-storage-id', {
cd69cedf
TL
313 completion => \&PVE::Storage::complete_storage,
314 }),
b6cf0a66
DM
315 },
316 },
317 returns => { type => 'null' },
318 code => sub {
319 my ($param) = @_;
320
1dc01b9f
DM
321 my $storeid = extract_param($param, 'storage');
322
8ff8e277
TL
323 PVE::Storage::lock_storage_config(sub {
324 my $cfg = PVE::Storage::config();
b6cf0a66 325
8ff8e277 326 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
402df80b 327
8ff8e277
TL
328 die "can't remove storage - storage is used as base of another storage\n"
329 if PVE::Storage::storage_is_used($cfg, $storeid);
b6cf0a66 330
8ff8e277 331 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
3932ca0d 332
8ff8e277 333 $plugin->on_delete_hook($storeid, $scfg);
3932ca0d 334
8ff8e277 335 delete $cfg->{ids}->{$storeid};
b6cf0a66 336
8ff8e277 337 PVE::Storage::write_config($cfg);
b6cf0a66 338
8ff8e277 339 }, "delete storage failed");
2a2cf20a
AG
340
341 PVE::AccessControl::remove_storage_access($storeid);
342
b6cf0a66
DM
343 return undef;
344 }});
345
3461;