@@ -76,6 +76,11 @@ public void setupRoutes(Router router) {
7676 this .handleUpdateKeypair (ctx );
7777 }
7878 }, new AuditParams (Collections .emptyList (), List .of ("subscription_id" , "name" , "contact" , "disabled" )), Role .MAINTAINER , Role .SHARING_PORTAL ));
79+ router .post (API_CLIENT_SIDE_KEYPAIRS_DELETE .toString ()).blockingHandler (auth .handle ((ctx ) -> {
80+ synchronized (writeLock ) {
81+ this .handleDeleteKeypair (ctx );
82+ }
83+ }, new AuditParams (Collections .emptyList (), List .of ("subscription_id" )), Role .PRIVILEGED , Role .SHARING_PORTAL ));
7984 router .get (API_CLIENT_SIDE_KEYPAIRS_LIST .toString ()).handler (
8085 auth .handle (this ::handleListAllKeypairs , Role .MAINTAINER , Role .METRICS_EXPORT ));
8186 router .get (API_CLIENT_SIDE_KEYPAIRS_SUBSCRIPTIONID .toString ()).handler (
@@ -119,22 +124,16 @@ private void handleAddKeypair(RoutingContext rc) {
119124 }
120125
121126 private void handleUpdateKeypair (RoutingContext rc ) {
122- final JsonObject body = rc .body ().asJsonObject ();
123- final String subscriptionId = body .getString ("subscription_id" );
127+ JsonObject body = getRequestBody (rc );
128+ if (body == null ) return ;
129+
124130 String contact = body .getString ("contact" );
125131 Boolean disabled = body .getBoolean ("disabled" );
126132 String name = body .getString ("name" );
127133
128- if (subscriptionId == null ) {
129- ResponseUtil .error (rc , 400 , "Required parameters: subscription_id" );
130- return ;
131- }
134+ ClientSideKeypair keypair = validateAndGetKeypair (rc , body );
135+ if (keypair == null ) return ;
132136
133- ClientSideKeypair keypair = this .keypairStore .getSnapshot ().getKeypair (subscriptionId );
134- if (keypair == null ) {
135- ResponseUtil .error (rc , 404 , "Failed to find a keypair for subscription id: " + subscriptionId );
136- return ;
137- }
138137
139138 if (contact == null && disabled == null && name == null ) {
140139 ResponseUtil .error (rc , 400 , "Updatable parameters: contact, disabled, name" );
@@ -179,6 +178,30 @@ private void handleUpdateKeypair(RoutingContext rc) {
179178 .end (json .encode ());
180179 }
181180
181+ private void handleDeleteKeypair (RoutingContext rc ) {
182+ JsonObject body = getRequestBody (rc );
183+ if (body == null ) return ;
184+
185+ ClientSideKeypair keypair = validateAndGetKeypair (rc , body );
186+ if (keypair == null ) return ;
187+
188+ Set <ClientSideKeypair > allKeypairs = new HashSet <>(this .keypairStore .getAll ());
189+ allKeypairs .remove (keypair );
190+
191+ try {
192+ storeWriter .upload (allKeypairs , null );
193+ } catch (Exception e ) {
194+ ResponseUtil .errorInternal (rc , "failed to upload keypairs" , e );
195+ return ;
196+ }
197+
198+ JsonObject responseJson = new JsonObject ()
199+ .put ("success" , true )
200+ .put ("deleted_keypair" , createKeypairJsonObject (toJsonWithoutPrivateKey (keypair )));
201+ rc .response ().putHeader (HttpHeaders .CONTENT_TYPE , "application/json" )
202+ .end (responseJson .encode ());
203+ }
204+
182205 public Iterable <ClientSideKeypair > getKeypairsBySite (int siteId ) {
183206 return this .keypairStore .getSnapshot ().getSiteKeypairs (siteId );
184207 }
@@ -243,4 +266,27 @@ public ClientSideKeypair createAndSaveSiteKeypair(int siteId, String contact, bo
243266 return newKeypair ;
244267
245268 }
269+
270+ private JsonObject getRequestBody (RoutingContext rc ) {
271+ JsonObject body = rc .body () != null ? rc .body ().asJsonObject () : null ;
272+ if (body == null ) {
273+ ResponseUtil .error (rc , 400 , "json payload required but not provided" );
274+ }
275+ return body ;
276+ }
277+
278+ private ClientSideKeypair validateAndGetKeypair (RoutingContext rc , JsonObject body ) {
279+ String subscriptionId = body .getString ("subscription_id" );
280+ if (subscriptionId == null ) {
281+ ResponseUtil .error (rc , 400 , "Required parameters: subscription_id" );
282+ return null ;
283+ }
284+
285+ ClientSideKeypair keypair = this .keypairStore .getSnapshot ().getKeypair (subscriptionId );
286+ if (keypair == null ) {
287+ ResponseUtil .error (rc , 404 , "Failed to find a keypair for subscription id: " + subscriptionId );
288+ return null ;
289+ }
290+ return keypair ;
291+ }
246292}
0 commit comments