]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/ACMEPlugin.pm
1 package PVE
::API2
::ACMEPlugin
;
7 use Storable
qw(dclone);
9 use PVE
::ACME
::Challenge
;
10 use PVE
::ACME
::DNSChallenge
;
11 use PVE
::ACME
::StandAlone
;
12 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_register_file cfs_lock_file);
13 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
14 use PVE
::Tools
qw(extract_param);
16 use base
qw(PVE::RESTHandler);
18 my $plugin_config_file = "priv/acme/plugins.cfg";
20 cfs_register_file
($plugin_config_file,
21 sub { PVE
::ACME
::Challenge-
>parse_config(@_); },
22 sub { PVE
::ACME
::Challenge-
>write_config(@_); },
25 PVE
::ACME
::DNSChallenge-
>register();
26 PVE
::ACME
::StandAlone-
>register();
27 PVE
::ACME
::Challenge-
>init();
29 PVE
::JSONSchema
::register_standard_option
('pve-acme-pluginid', {
31 format
=> 'pve-configid',
32 description
=> 'Unique identifier for ACME plugin instance.',
35 my $plugin_type_enum = PVE
::ACME
::Challenge-
>lookup_types();
37 my $modify_cfg_for_api = sub {
38 my ($cfg, $pluginid) = @_;
40 die "ACME plugin '$pluginid' not defined\n" if !defined($cfg->{ids
}->{$pluginid});
42 my $plugin_cfg = dclone
($cfg->{ids
}->{$pluginid});
43 $plugin_cfg->{plugin
} = $pluginid;
44 $plugin_cfg->{digest
} = $cfg->{digest
};
49 __PACKAGE__-
>register_method ({
54 check
=> ['perm', '/', [ 'Sys.Modify' ]],
56 description
=> "ACME plugin index.",
59 additionalProperties
=> 0,
62 description
=> "Only list ACME plugins of a specific type",
64 enum
=> $plugin_type_enum,
74 plugin
=> get_standard_option
('pve-acme-pluginid'),
77 links
=> [ { rel
=> 'child', href
=> "{plugin}" } ],
82 my $cfg = load_config
();
85 foreach my $pluginid (keys %{$cfg->{ids
}}) {
86 my $plugin_cfg = $modify_cfg_for_api->($cfg, $pluginid);
87 next if $param->{type
} && $param->{type
} ne $plugin_cfg->{type
};
88 push @$res, $plugin_cfg;
95 __PACKAGE__-
>register_method({
96 name
=> 'get_plugin_config',
99 description
=> "Get ACME plugin configuration.",
101 check
=> ['perm', '/', [ 'Sys.Modify' ]],
105 additionalProperties
=> 0,
107 id
=> get_standard_option
('pve-acme-pluginid'),
116 my $cfg = load_config
();
117 return $modify_cfg_for_api->($cfg, $param->{id
});
121 __PACKAGE__-
>register_method({
122 name
=> 'add_plugin',
125 description
=> "Add ACME plugin configuration.",
127 check
=> ['perm', '/', [ 'Sys.Modify' ]],
130 parameters
=> PVE
::ACME
::Challenge-
>createSchema(),
137 my $id = extract_param
($param, 'id');
138 my $type = extract_param
($param, 'type');
140 cfs_lock_file
($plugin_config_file, undef, sub {
141 my $cfg = load_config
();
142 die "ACME plugin ID '$id' already exists\n" if defined($cfg->{ids
}->{$id});
144 my $plugin = PVE
::ACME
::Challenge-
>lookup($type);
145 my $opts = $plugin->check_config($id, $param, 1, 1);
147 $cfg->{ids
}->{$id} = $opts;
148 $cfg->{ids
}->{$id}->{type
} = $type;
150 cfs_write_file
($plugin_config_file, $cfg);
158 __PACKAGE__-
>register_method({
159 name
=> 'update_plugin',
162 description
=> "Update ACME plugin configuration.",
164 check
=> ['perm', '/', [ 'Sys.Modify' ]],
167 parameters
=> PVE
::ACME
::Challenge-
>updateSchema(),
174 my $id = extract_param
($param, 'id');
175 my $delete = extract_param
($param, 'delete');
176 my $digest = extract_param
($param, 'digest');
178 cfs_lock_file
($plugin_config_file, undef, sub {
179 my $cfg = load_config
();
180 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest);
181 my $plugin_cfg = $cfg->{ids
}->{$id};
182 die "ACME plugin ID '$id' does not exist\n" if !$plugin_cfg;
184 my $type = $plugin_cfg->{type
};
185 my $plugin = PVE
::ACME
::Challenge-
>lookup($type);
187 if (defined($delete)) {
188 my $schema = $plugin->private();
189 my $options = $schema->{options
}->{$type};
190 for my $k (PVE
::Tools
::split_list
($delete)) {
191 my $d = $options->{$k} || die "no such option '$k'\n";
192 die "unable to delete required option '$k'\n" if !$d->{optional
};
194 delete $cfg->{ids
}->{$id}->{$k};
198 my $opts = $plugin->check_config($id, $param, 0, 1);
199 for my $k (sort keys %$opts) {
200 $plugin_cfg->{$k} = $opts->{$k};
203 cfs_write_file
($plugin_config_file, $cfg);
211 __PACKAGE__-
>register_method({
212 name
=> 'delete_plugin',
215 description
=> "Delete ACME plugin configuration.",
217 check
=> ['perm', '/', [ 'Sys.Modify' ]],
221 additionalProperties
=> 0,
223 id
=> get_standard_option
('pve-acme-pluginid'),
232 my $id = extract_param
($param, 'id');
234 cfs_lock_file
($plugin_config_file, undef, sub {
235 my $cfg = load_config
();
237 delete $cfg->{ids
}->{$id};
239 cfs_write_file
($plugin_config_file, $cfg);
248 # auto-adds the standalone plugin if no config is there for backwards
249 # compatibility, so ALWAYS call the cfs registered parser
250 return cfs_read_file
($plugin_config_file);