forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEC2RestServlet.java
More file actions
2235 lines (1936 loc) · 104 KB
/
EC2RestServlet.java
File metadata and controls
2235 lines (1936 loc) · 104 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.bridge.service;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.security.KeyStore;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.inject.Inject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.databinding.ADBBean;
import org.apache.axis2.databinding.ADBException;
import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.amazon.ec2.AllocateAddressResponse;
import com.amazon.ec2.AssociateAddressResponse;
import com.amazon.ec2.AttachVolumeResponse;
import com.amazon.ec2.AuthorizeSecurityGroupIngressResponse;
import com.amazon.ec2.CreateImageResponse;
import com.amazon.ec2.CreateKeyPairResponse;
import com.amazon.ec2.CreateSecurityGroupResponse;
import com.amazon.ec2.CreateSnapshotResponse;
import com.amazon.ec2.CreateTagsResponse;
import com.amazon.ec2.CreateVolumeResponse;
import com.amazon.ec2.DeleteKeyPairResponse;
import com.amazon.ec2.DeleteSecurityGroupResponse;
import com.amazon.ec2.DeleteSnapshotResponse;
import com.amazon.ec2.DeleteTagsResponse;
import com.amazon.ec2.DeleteVolumeResponse;
import com.amazon.ec2.DeregisterImageResponse;
import com.amazon.ec2.DescribeAvailabilityZonesResponse;
import com.amazon.ec2.DescribeImageAttributeResponse;
import com.amazon.ec2.DescribeImagesResponse;
import com.amazon.ec2.DescribeInstanceAttributeResponse;
import com.amazon.ec2.DescribeInstancesResponse;
import com.amazon.ec2.DescribeKeyPairsResponse;
import com.amazon.ec2.DescribeSecurityGroupsResponse;
import com.amazon.ec2.DescribeSnapshotsResponse;
import com.amazon.ec2.DescribeTagsResponse;
import com.amazon.ec2.DescribeVolumesResponse;
import com.amazon.ec2.DetachVolumeResponse;
import com.amazon.ec2.DisassociateAddressResponse;
import com.amazon.ec2.GetPasswordDataResponse;
import com.amazon.ec2.ImportKeyPairResponse;
import com.amazon.ec2.ModifyImageAttributeResponse;
import com.amazon.ec2.ModifyInstanceAttributeResponse;
import com.amazon.ec2.RebootInstancesResponse;
import com.amazon.ec2.RegisterImageResponse;
import com.amazon.ec2.ReleaseAddressResponse;
import com.amazon.ec2.ResetImageAttributeResponse;
import com.amazon.ec2.RevokeSecurityGroupIngressResponse;
import com.amazon.ec2.RunInstancesResponse;
import com.amazon.ec2.StartInstancesResponse;
import com.amazon.ec2.StopInstancesResponse;
import com.amazon.ec2.TerminateInstancesResponse;
import com.cloud.bridge.model.UserCredentialsVO;
import com.cloud.bridge.persist.dao.CloudStackUserDaoImpl;
import com.cloud.bridge.persist.dao.OfferingDaoImpl;
import com.cloud.bridge.persist.dao.UserCredentialsDaoImpl;
import com.cloud.bridge.service.controller.s3.ServiceProvider;
import com.cloud.bridge.service.core.ec2.EC2AddressFilterSet;
import com.cloud.bridge.service.core.ec2.EC2AssociateAddress;
import com.cloud.bridge.service.core.ec2.EC2AuthorizeRevokeSecurityGroup;
import com.cloud.bridge.service.core.ec2.EC2AvailabilityZonesFilterSet;
import com.cloud.bridge.service.core.ec2.EC2CreateImage;
import com.cloud.bridge.service.core.ec2.EC2CreateKeyPair;
import com.cloud.bridge.service.core.ec2.EC2CreateVolume;
import com.cloud.bridge.service.core.ec2.EC2DeleteKeyPair;
import com.cloud.bridge.service.core.ec2.EC2DescribeAddresses;
import com.cloud.bridge.service.core.ec2.EC2DescribeAvailabilityZones;
import com.cloud.bridge.service.core.ec2.EC2DescribeImageAttribute;
import com.cloud.bridge.service.core.ec2.EC2DescribeImages;
import com.cloud.bridge.service.core.ec2.EC2DescribeInstances;
import com.cloud.bridge.service.core.ec2.EC2DescribeKeyPairs;
import com.cloud.bridge.service.core.ec2.EC2DescribeSecurityGroups;
import com.cloud.bridge.service.core.ec2.EC2DescribeSnapshots;
import com.cloud.bridge.service.core.ec2.EC2DescribeTags;
import com.cloud.bridge.service.core.ec2.EC2DescribeVolumes;
import com.cloud.bridge.service.core.ec2.EC2DisassociateAddress;
import com.cloud.bridge.service.core.ec2.EC2Engine;
import com.cloud.bridge.service.core.ec2.EC2Filter;
import com.cloud.bridge.service.core.ec2.EC2GroupFilterSet;
import com.cloud.bridge.service.core.ec2.EC2Image;
import com.cloud.bridge.service.core.ec2.EC2ImageFilterSet;
import com.cloud.bridge.service.core.ec2.EC2ImageAttributes.ImageAttribute;
import com.cloud.bridge.service.core.ec2.EC2ImageLaunchPermission;
import com.cloud.bridge.service.core.ec2.EC2ImportKeyPair;
import com.cloud.bridge.service.core.ec2.EC2InstanceFilterSet;
import com.cloud.bridge.service.core.ec2.EC2IpPermission;
import com.cloud.bridge.service.core.ec2.EC2KeyPairFilterSet;
import com.cloud.bridge.service.core.ec2.EC2ModifyImageAttribute;
import com.cloud.bridge.service.core.ec2.EC2ModifyInstanceAttribute;
import com.cloud.bridge.service.core.ec2.EC2RebootInstances;
import com.cloud.bridge.service.core.ec2.EC2RegisterImage;
import com.cloud.bridge.service.core.ec2.EC2ReleaseAddress;
import com.cloud.bridge.service.core.ec2.EC2RunInstances;
import com.cloud.bridge.service.core.ec2.EC2SecurityGroup;
import com.cloud.bridge.service.core.ec2.EC2SnapshotFilterSet;
import com.cloud.bridge.service.core.ec2.EC2StartInstances;
import com.cloud.bridge.service.core.ec2.EC2StopInstances;
import com.cloud.bridge.service.core.ec2.EC2Tags;
import com.cloud.bridge.service.core.ec2.EC2TagsFilterSet;
import com.cloud.bridge.service.core.ec2.EC2Volume;
import com.cloud.bridge.service.core.ec2.EC2VolumeFilterSet;
import com.cloud.bridge.service.exception.EC2ServiceException;
import com.cloud.bridge.service.exception.EC2ServiceException.ClientError;
import com.cloud.bridge.service.exception.NoSuchObjectException;
import com.cloud.bridge.service.exception.PermissionDeniedException;
import com.cloud.bridge.util.AuthenticationUtils;
import com.cloud.bridge.util.ConfigurationHelper;
import com.cloud.bridge.util.EC2RestAuth;
import com.cloud.stack.models.CloudStackAccount;
import com.cloud.utils.db.Transaction;
@Component("EC2RestServlet")
public class EC2RestServlet extends HttpServlet {
private static final long serialVersionUID = -6168996266762804888L;
@Inject UserCredentialsDaoImpl ucDao;
@Inject OfferingDaoImpl ofDao;
@Inject CloudStackUserDaoImpl userDao;
public static final Logger logger = Logger.getLogger(EC2RestServlet.class);
private final OMFactory factory = OMAbstractFactory.getOMFactory();
private final XMLOutputFactory xmlOutFactory = XMLOutputFactory.newInstance();
private String pathToKeystore = null;
private String keystorePassword = null;
private String wsdlVersion = null;
private String version = null;
boolean debug=true;
public EC2RestServlet() {
}
/**
* We build the path to where the keystore holding the WS-Security X509 certificates
* are stored.
*/
@Override
public void init( ServletConfig config ) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
File propertiesFile = ConfigurationHelper.findConfigurationFile("ec2-service.properties");
Properties EC2Prop = null;
if (null != propertiesFile) {
logger.info("Use EC2 properties file: " + propertiesFile.getAbsolutePath());
EC2Prop = new Properties();
try {
EC2Prop.load( new FileInputStream( propertiesFile ));
} catch (FileNotFoundException e) {
logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e);
} catch (IOException e) {
logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e);
}
String keystore = EC2Prop.getProperty( "keystore" );
keystorePassword = EC2Prop.getProperty( "keystorePass" );
wsdlVersion = EC2Prop.getProperty( "WSDLVersion", "2012-08-15" );
version = EC2Prop.getProperty( "cloudbridgeVersion", "UNKNOWN VERSION" );
String installedPath = System.getenv("CATALINA_HOME");
if (installedPath == null) installedPath = System.getenv("CATALINA_BASE");
if (installedPath == null) installedPath = System.getProperty("catalina.home");
String webappPath = config.getServletContext().getRealPath("/");
//pathToKeystore = new String( installedPath + File.separator + "webapps" + File.separator + webappName + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + keystore );
pathToKeystore = new String( webappPath + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + keystore );
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
doGetOrPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGetOrPost(req, resp);
}
protected void doGetOrPost(HttpServletRequest request, HttpServletResponse response) {
if(debug){
System.out.println("EC2RestServlet.doGetOrPost: javax.servlet.forward.request_uri: "+request.getAttribute("javax.servlet.forward.request_uri"));
System.out.println("EC2RestServlet.doGetOrPost: javax.servlet.forward.context_path: "+request.getAttribute("javax.servlet.forward.context_path"));
System.out.println("EC2RestServlet.doGetOrPost: javax.servlet.forward.servlet_path: "+request.getAttribute("javax.servlet.forward.servlet_path"));
System.out.println("EC2RestServlet.doGetOrPost: javax.servlet.forward.path_info: "+request.getAttribute("javax.servlet.forward.path_info"));
System.out.println("EC2RestServlet.doGetOrPost: javax.servlet.forward.query_string: "+request.getAttribute("javax.servlet.forward.query_string"));
}
String action = request.getParameter( "Action" );
logRequest(request);
// -> unauthenticated calls, should still be done over HTTPS
if (action.equalsIgnoreCase( "SetUserKeys" )) {
setUserKeys(request, response);
return;
}
if (action.equalsIgnoreCase( "CloudEC2Version" )) {
cloudEC2Version(request, response);
return;
}
// -> authenticated calls
try {
if (!authenticateRequest( request, response )) return;
if (action.equalsIgnoreCase( "AllocateAddress" )) allocateAddress(request, response);
else if (action.equalsIgnoreCase( "AssociateAddress" )) associateAddress(request, response);
else if (action.equalsIgnoreCase( "AttachVolume" )) attachVolume(request, response );
else if (action.equalsIgnoreCase( "AuthorizeSecurityGroupIngress" )) authorizeSecurityGroupIngress(request, response);
else if (action.equalsIgnoreCase( "CreateImage" )) createImage(request, response);
else if (action.equalsIgnoreCase( "CreateSecurityGroup" )) createSecurityGroup(request, response);
else if (action.equalsIgnoreCase( "CreateSnapshot" )) createSnapshot(request, response);
else if (action.equalsIgnoreCase( "CreateVolume" )) createVolume(request, response);
else if (action.equalsIgnoreCase( "DeleteSecurityGroup" )) deleteSecurityGroup(request, response);
else if (action.equalsIgnoreCase( "DeleteSnapshot" )) deleteSnapshot(request, response);
else if (action.equalsIgnoreCase( "DeleteVolume" )) deleteVolume(request, response);
else if (action.equalsIgnoreCase( "DeregisterImage" )) deregisterImage(request, response);
else if (action.equalsIgnoreCase( "DescribeAddresses" )) describeAddresses(request, response);
else if (action.equalsIgnoreCase( "DescribeAvailabilityZones" )) describeAvailabilityZones(request, response);
else if (action.equalsIgnoreCase( "DescribeImageAttribute" )) describeImageAttribute(request, response);
else if (action.equalsIgnoreCase( "DescribeImages" )) describeImages(request, response);
else if (action.equalsIgnoreCase( "DescribeInstanceAttribute" )) describeInstanceAttribute(request, response);
else if (action.equalsIgnoreCase( "DescribeInstances" )) describeInstances(request, response);
else if (action.equalsIgnoreCase( "DescribeSecurityGroups" )) describeSecurityGroups(request, response);
else if (action.equalsIgnoreCase( "DescribeSnapshots" )) describeSnapshots(request, response);
else if (action.equalsIgnoreCase( "DescribeVolumes" )) describeVolumes(request, response);
else if (action.equalsIgnoreCase( "DetachVolume" )) detachVolume(request, response);
else if (action.equalsIgnoreCase( "DisassociateAddress" )) disassociateAddress(request, response);
else if (action.equalsIgnoreCase( "ModifyImageAttribute" )) modifyImageAttribute(request, response);
else if (action.equalsIgnoreCase( "ModifyInstanceAttribute" )) modifyInstanceAttribute(request, response);
else if (action.equalsIgnoreCase( "RebootInstances" )) rebootInstances(request, response);
else if (action.equalsIgnoreCase( "RegisterImage" )) registerImage(request, response);
else if (action.equalsIgnoreCase( "ReleaseAddress" )) releaseAddress(request, response);
else if (action.equalsIgnoreCase( "ResetImageAttribute" )) resetImageAttribute(request, response);
else if (action.equalsIgnoreCase( "RevokeSecurityGroupIngress")) revokeSecurityGroupIngress(request, response);
else if (action.equalsIgnoreCase( "RunInstances" )) runInstances(request, response);
else if (action.equalsIgnoreCase( "StartInstances" )) startInstances(request, response);
else if (action.equalsIgnoreCase( "StopInstances" )) stopInstances(request, response);
else if (action.equalsIgnoreCase( "TerminateInstances" )) terminateInstances(request, response);
else if (action.equalsIgnoreCase( "SetCertificate" )) setCertificate(request, response);
else if (action.equalsIgnoreCase( "DeleteCertificate" )) deleteCertificate(request, response);
else if (action.equalsIgnoreCase( "SetOfferMapping" )) setOfferMapping(request, response);
else if (action.equalsIgnoreCase( "DeleteOfferMapping" )) deleteOfferMapping(request, response);
else if (action.equalsIgnoreCase( "CreateKeyPair" )) createKeyPair(request, response);
else if (action.equalsIgnoreCase( "ImportKeyPair" )) importKeyPair(request, response);
else if (action.equalsIgnoreCase( "DeleteKeyPair" )) deleteKeyPair(request, response);
else if (action.equalsIgnoreCase( "DescribeKeyPairs" )) describeKeyPairs(request, response);
else if (action.equalsIgnoreCase( "CreateTags" )) createTags(request, response);
else if (action.equalsIgnoreCase( "DeleteTags" )) deleteTags(request, response);
else if (action.equalsIgnoreCase( "DescribeTags" )) describeTags(request, response);
else if (action.equalsIgnoreCase( "GetPasswordData" )) getPasswordData(request, response);
else {
logger.error("Unsupported action " + action);
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
}
} catch( EC2ServiceException e ) {
response.setStatus(e.getErrorCode());
if (e.getCause() != null && e.getCause() instanceof AxisFault) {
String errorCode = ((AxisFault)e.getCause()).getFaultCode().getLocalPart();
if (errorCode.startsWith("Client.")) // only in a SOAP API client error code is prefixed with Client.
errorCode = errorCode.split("Client.")[1];
else if (errorCode.startsWith("Server.")) // only in a SOAP API server error code is prefixed with Server.
errorCode = errorCode.split("Server.")[1];
faultResponse(response, errorCode, e.getMessage());
}
else {
logger.error("EC2ServiceException: " + e.getMessage(), e);
endResponse(response, e.toString());
}
} catch( PermissionDeniedException e ) {
logger.error("Unexpected exception: " + e.getMessage(), e);
response.setStatus(403);
endResponse(response, "Access denied");
} catch( Exception e ) {
logger.error("Unexpected exception: " + e.getMessage(), e);
response.setStatus(500);
endResponse(response, e.toString());
} finally {
try {
response.flushBuffer();
} catch (IOException e) {
logger.error("Unexpected exception " + e.getMessage(), e);
}
}
}
/**
* Provide an easy way to determine the version of the implementation running.
*
* This is an unauthenticated REST call.
*/
private void cloudEC2Version( HttpServletRequest request, HttpServletResponse response ) {
String version_response = new String( "<?xml version=\"1.0\" encoding=\"utf-8\"?><CloudEC2Version>" + version + "</CloudEC2Version>" );
response.setStatus(200);
endResponse(response, version_response);
}
/**
* This request registers the Cloud.com account holder to the EC2 service. The Cloud.com
* account holder saves his API access and secret keys with the EC2 service so that
* the EC2 service can make Cloud.com API calls on his behalf. The given API access
* and secret key are saved into the "usercredentials" database table.
*
* This is an unauthenticated REST call. The only required parameters are 'accesskey' and
* 'secretkey'.
*
* To verify that the given keys represent an existing account they are used to execute the
* Cloud.com's listAccounts API function. If the keys do not represent a valid account the
* listAccounts function will fail.
*
* A user can call this REST function any number of times, on each call the Cloud.com secret
* key is simply over writes any previously stored value.
*
* As with all REST calls HTTPS should be used to ensure their security.
*/
private void setUserKeys( HttpServletRequest request, HttpServletResponse response ) {
String[] accessKey = null;
String[] secretKey = null;
Transaction txn = null;
try {
// -> all these parameters are required
accessKey = request.getParameterValues( "accesskey" );
if ( null == accessKey || 0 == accessKey.length ) {
response.sendError(530, "Missing accesskey parameter" );
return;
}
secretKey = request.getParameterValues( "secretkey" );
if ( null == secretKey || 0 == secretKey.length ) {
response.sendError(530, "Missing secretkey parameter" );
return;
}
} catch( Exception e ) {
logger.error("SetUserKeys exception " + e.getMessage(), e);
response.setStatus(500);
endResponse(response, "SetUserKeys exception " + e.getMessage());
return;
}
try {
txn = Transaction.open(Transaction.AWSAPI_DB);
// -> use the keys to see if the account actually exists
ServiceProvider.getInstance().getEC2Engine().validateAccount( accessKey[0], secretKey[0] );
/* UserCredentialsDao credentialDao = new UserCredentialsDao();
credentialDao.setUserKeys( );
*/ UserCredentialsVO user = new UserCredentialsVO(accessKey[0], secretKey[0]);
ucDao.persist(user);
txn.commit();
} catch( Exception e ) {
logger.error("SetUserKeys " + e.getMessage(), e);
response.setStatus(401);
endResponse(response, e.toString());
txn.close();
return;
}
response.setStatus(200);
endResponse(response, "User keys set successfully");
}
/**
* The SOAP API for EC2 uses WS-Security to sign all client requests. This requires that
* the client have a public/private key pair and the public key defined by a X509 certificate.
* Thus in order for a Cloud.com account holder to use the EC2's SOAP API he must register
* his X509 certificate with the EC2 service. This function allows the Cloud.com account
* holder to "load" his X509 certificate into the service. Note, that the SetUserKeys REST
* function must be called before this call.
*
* This is an authenticated REST call and as such must contain all the required REST parameters
* including: Signature, Timestamp, Expires, etc. The signature is calculated using the
* Cloud.com account holder's API access and secret keys and the Amazon defined EC2 signature
* algorithm.
*
* A user can call this REST function any number of times, on each call the X509 certificate
* simply over writes any previously stored value.
*/
private void setCertificate( HttpServletRequest request, HttpServletResponse response )
throws Exception {
Transaction txn = null;
try {
// [A] Pull the cert and cloud AccessKey from the request
String[] certificate = request.getParameterValues( "cert" );
if (null == certificate || 0 == certificate.length) {
response.sendError(530, "Missing cert parameter" );
return;
}
// logger.debug( "SetCertificate cert: [" + certificate[0] + "]" );
String [] accessKey = request.getParameterValues( "AWSAccessKeyId" );
if ( null == accessKey || 0 == accessKey.length ) {
response.sendError(530, "Missing AWSAccessKeyId parameter" );
return;
}
// [B] Open our keystore
FileInputStream fsIn = new FileInputStream( pathToKeystore );
KeyStore certStore = KeyStore.getInstance( "JKS" );
certStore.load( fsIn, keystorePassword.toCharArray());
// -> use the Cloud API key to save the cert in the keystore
// -> write the cert into the keystore on disk
Certificate userCert = null;
CertificateFactory cf = CertificateFactory.getInstance( "X.509" );
ByteArrayInputStream bs = new ByteArrayInputStream( certificate[0].getBytes());
while (bs.available() > 0) userCert = cf.generateCertificate(bs);
certStore.setCertificateEntry( accessKey[0], userCert );
FileOutputStream fsOut = new FileOutputStream( pathToKeystore );
certStore.store( fsOut, keystorePassword.toCharArray());
// [C] Associate the cert's uniqueId with the Cloud API keys
String uniqueId = AuthenticationUtils.X509CertUniqueId( userCert );
logger.debug( "SetCertificate, uniqueId: " + uniqueId );
/* UserCredentialsDao credentialDao = new UserCredentialsDao();
credentialDao.setCertificateId( accessKey[0], uniqueId );
*/
txn = Transaction.open(Transaction.AWSAPI_DB);
UserCredentialsVO user = ucDao.getByAccessKey(accessKey[0]);
user.setCertUniqueId(uniqueId);
ucDao.update(user.getId(), user);
response.setStatus(200);
endResponse(response, "User certificate set successfully");
txn.commit();
} catch( NoSuchObjectException e ) {
logger.error("SetCertificate exception " + e.getMessage(), e);
response.sendError(404, "SetCertificate exception " + e.getMessage());
} catch( Exception e ) {
logger.error("SetCertificate exception " + e.getMessage(), e);
response.sendError(500, "SetCertificate exception " + e.getMessage());
} finally {
txn.close();
}
}
/**
* The SOAP API for EC2 uses WS-Security to sign all client requests. This requires that
* the client have a public/private key pair and the public key defined by a X509 certificate.
* This REST call allows a Cloud.com account holder to remove a previouly "loaded" X509
* certificate out of the EC2 service.
*
* This is an unauthenticated REST call and as such must contain all the required REST parameters
* including: Signature, Timestamp, Expires, etc. The signature is calculated using the
* Cloud.com account holder's API access and secret keys and the Amazon defined EC2 signature
* algorithm.
*/
private void deleteCertificate( HttpServletRequest request, HttpServletResponse response )
throws Exception {
Transaction txn = null;
try {
String [] accessKey = request.getParameterValues( "AWSAccessKeyId" );
if ( null == accessKey || 0 == accessKey.length ) {
response.sendError(530, "Missing AWSAccessKeyId parameter" );
return;
}
// -> delete the specified entry and save back to disk
FileInputStream fsIn = new FileInputStream( pathToKeystore );
KeyStore certStore = KeyStore.getInstance( "JKS" );
certStore.load( fsIn, keystorePassword.toCharArray());
if ( certStore.containsAlias( accessKey[0] )) {
certStore.deleteEntry( accessKey[0] );
FileOutputStream fsOut = new FileOutputStream( pathToKeystore );
certStore.store( fsOut, keystorePassword.toCharArray());
// -> dis-associate the cert's uniqueId with the Cloud API keys
/* UserCredentialsDao credentialDao = new UserCredentialsDao();
credentialDao.setCertificateId( accessKey[0], null );
*/ txn = Transaction.open(Transaction.AWSAPI_DB);
UserCredentialsVO user = ucDao.getByAccessKey(accessKey[0]);
user.setCertUniqueId(null);
ucDao.update(user.getId(), user);
response.setStatus(200);
endResponse(response, "User certificate deleted successfully");
txn.commit();
}
else response.setStatus(404);
} catch( NoSuchObjectException e ) {
logger.error("SetCertificate exception " + e.getMessage(), e);
response.sendError(404, "SetCertificate exception " + e.getMessage());
} catch( Exception e ) {
logger.error("DeleteCertificate exception " + e.getMessage(), e);
response.sendError(500, "DeleteCertificate exception " + e.getMessage());
} finally {
txn.close();
}
}
/**
* Allow the caller to define the mapping between the Amazon instance type strings
* (e.g., m1.small, cc1.4xlarge) and the cloudstack service offering ids. Setting
* an existing mapping just over writes the prevous values.
*/
private void setOfferMapping( HttpServletRequest request, HttpServletResponse response ) {
String amazonOffer = null;
String cloudOffer = null;
try {
// -> all these parameters are required
amazonOffer = request.getParameter( "amazonoffer" );
if ( null == amazonOffer ) {
response.sendError(530, "Missing amazonoffer parameter" );
return;
}
cloudOffer = request.getParameter( "cloudoffer" );
if ( null == cloudOffer ) {
response.sendError(530, "Missing cloudoffer parameter" );
return;
}
} catch( Exception e ) {
logger.error("SetOfferMapping exception " + e.getMessage(), e);
response.setStatus(500);
endResponse(response, "SetOfferMapping exception " + e.getMessage());
return;
}
// validate account is admin level
try {
CloudStackAccount currentAccount = ServiceProvider.getInstance().getEC2Engine().getCurrentAccount();
if (currentAccount.getAccountType() != 1) {
logger.debug("SetOfferMapping called by non-admin user!");
response.setStatus(500);
endResponse(response, "Permission denied for non-admin user to setOfferMapping!");
return;
}
} catch (Exception e) {
logger.error("SetOfferMapping " + e.getMessage(), e);
response.setStatus(401);
endResponse(response, e.toString());
return;
}
try {
ofDao.setOfferMapping( amazonOffer, cloudOffer );
} catch( Exception e ) {
logger.error("SetOfferMapping " + e.getMessage(), e);
response.setStatus(401);
endResponse(response, e.toString());
return;
}
response.setStatus(200);
endResponse(response, "offering mapping set successfully");
}
private void deleteOfferMapping( HttpServletRequest request, HttpServletResponse response ) {
String amazonOffer = null;
try {
// -> all these parameters are required
amazonOffer = request.getParameter( "amazonoffer" );
if ( null == amazonOffer ) {
response.sendError(530, "Missing amazonoffer parameter" );
return;
}
} catch( Exception e ) {
logger.error("DeleteOfferMapping exception " + e.getMessage(), e);
response.setStatus(500);
endResponse(response, "DeleteOfferMapping exception " + e.getMessage());
return;
}
// validate account is admin level
try {
CloudStackAccount currentAccount = ServiceProvider.getInstance().getEC2Engine().getCurrentAccount();
if (currentAccount.getAccountType() != 1) {
logger.debug("deleteOfferMapping called by non-admin user!");
response.setStatus(500);
endResponse(response, "Permission denied for non-admin user to deleteOfferMapping!");
return;
}
} catch (Exception e) {
logger.error("deleteOfferMapping " + e.getMessage(), e);
response.setStatus(401);
endResponse(response, e.toString());
return;
}
try {
ofDao.deleteOfferMapping( amazonOffer );
} catch( Exception e ) {
logger.error("DeleteOfferMapping " + e.getMessage(), e);
response.setStatus(401);
endResponse(response, e.toString());
return;
}
response.setStatus(200);
endResponse(response, "offering mapping deleted successfully");
}
/**
* The approach taken here is to map these REST calls into the same objects used
* to implement the matching SOAP requests (e.g., AttachVolume). This is done by parsing
* out the URL parameters and loading them into the relevant EC2XXX object(s). Once
* the parameters are loaded the appropriate EC2Engine function is called to perform
* the requested action. The result of the EC2Engine function is a standard
* Amazon WSDL defined object (e.g., AttachVolumeResponse Java object). Finally the
* serialize method is called on the returned response object to obtain the extected
* response XML.
*/
private void attachVolume( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
EC2Volume EC2request = new EC2Volume();
// -> all these parameters are required
String[] volumeId = request.getParameterValues( "VolumeId" );
if ( null != volumeId && 0 < volumeId.length )
EC2request.setId( volumeId[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - VolumeId");
}
String[] instanceId = request.getParameterValues( "InstanceId" );
if ( null != instanceId && 0 < instanceId.length )
EC2request.setInstanceId( instanceId[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - InstanceId");
}
String[] device = request.getParameterValues( "Device" );
if ( null != device && 0 < device.length )
EC2request.setDevice( device[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - Device");
}
// -> execute the request
AttachVolumeResponse EC2response = EC2SoapServiceImpl.toAttachVolumeResponse( ServiceProvider.getInstance().getEC2Engine().attachVolume( EC2request ));
serializeResponse(response, EC2response);
}
/**
* The SOAP equivalent of this function appears to allow multiple permissions per request, yet
* in the REST API documentation only one permission is allowed.
*/
private void revokeSecurityGroupIngress( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
EC2AuthorizeRevokeSecurityGroup EC2request = new EC2AuthorizeRevokeSecurityGroup();
String[] groupName = request.getParameterValues( "GroupName" );
if ( null != groupName && 0 < groupName.length )
EC2request.setName( groupName[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - GroupName");
}
// -> not clear how many parameters there are until we fail to get IpPermissions.n.IpProtocol
int nCount = 1, mCount;
do {
EC2IpPermission perm = new EC2IpPermission();
String[] protocol = request.getParameterValues( "IpPermissions." + nCount + ".IpProtocol" );
if ( null != protocol && 0 < protocol.length )
perm.setProtocol( protocol[0]);
else break;
String[] fromPort = request.getParameterValues( "IpPermissions." + nCount + ".FromPort" );
if ( null != fromPort && 0 < fromPort.length ) {
if ( protocol[0].equalsIgnoreCase("icmp") )
perm.setIcmpType( fromPort[0] ) ;
else
perm.setFromPort( Integer.parseInt( fromPort[0]) );
}
String[] toPort = request.getParameterValues( "IpPermissions." + nCount + ".ToPort" );
if ( null != toPort && 0 < toPort.length ) {
if ( protocol[0].equalsIgnoreCase("icmp") )
perm.setIcmpCode( toPort[0] );
else
perm.setToPort( Integer.parseInt( toPort[0]) );
}
// -> list: IpPermissions.n.IpRanges.m.CidrIp
mCount = 1;
do {
String[] ranges = request.getParameterValues( "IpPermissions." + nCount + ".IpRanges." + mCount + ".CidrIp" );
if ( null != ranges && 0 < ranges.length)
perm.addIpRange( ranges[0]);
else break;
mCount++;
} while( true );
// -> list: IpPermissions.n.Groups.m.UserId and IpPermissions.n.Groups.m.GroupName
mCount = 1;
do {
EC2SecurityGroup group = new EC2SecurityGroup();
String[] user = request.getParameterValues( "IpPermissions." + nCount + ".Groups." + mCount + ".UserId" );
if ( null != user && 0 < user.length)
group.setAccount( user[0]);
else break;
String[] name = request.getParameterValues( "IpPermissions." + nCount + ".Groups." + mCount + ".GroupName" );
if ( null != name && 0 < name.length)
group.setName( name[0]);
else break;
perm.addUser( group);
mCount++;
} while( true );
// -> multiple IP permissions can be specified per group name
EC2request.addIpPermission( perm);
nCount++;
} while( true );
if (1 == nCount) {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - IpPermissions");
}
// -> execute the request
RevokeSecurityGroupIngressResponse EC2response = EC2SoapServiceImpl.toRevokeSecurityGroupIngressResponse(
ServiceProvider.getInstance().getEC2Engine().revokeSecurityGroup( EC2request ));
serializeResponse(response, EC2response);
}
private void authorizeSecurityGroupIngress( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
// -> parse the complicated paramters into our standard object
EC2AuthorizeRevokeSecurityGroup EC2request = new EC2AuthorizeRevokeSecurityGroup();
String[] groupName = request.getParameterValues( "GroupName" );
if ( null != groupName && 0 < groupName.length )
EC2request.setName( groupName[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter 'Groupname'");
}
// -> not clear how many parameters there are until we fail to get IpPermissions.n.IpProtocol
int nCount = 1;
do
{ EC2IpPermission perm = new EC2IpPermission();
String[] protocol = request.getParameterValues( "IpPermissions." + nCount + ".IpProtocol" );
if ( null != protocol && 0 < protocol.length )
perm.setProtocol( protocol[0] );
else break;
String[] fromPort = request.getParameterValues( "IpPermissions." + nCount + ".FromPort" );
if ( null != fromPort && 0 < fromPort.length ) {
if ( protocol[0].equalsIgnoreCase("icmp") )
perm.setIcmpType( fromPort[0] ) ;
else
perm.setFromPort( Integer.parseInt( fromPort[0]) );
}
String[] toPort = request.getParameterValues( "IpPermissions." + nCount + ".ToPort" );
if ( null != toPort && 0 < toPort.length ) {
if ( protocol[0].equalsIgnoreCase("icmp") )
perm.setIcmpCode( toPort[0] );
else
perm.setToPort( Integer.parseInt( toPort[0]) );
}
// -> list: IpPermissions.n.IpRanges.m.CidrIp
int mCount = 1;
do
{ String[] ranges = request.getParameterValues( "IpPermissions." + nCount + ".IpRanges." + mCount + ".CidrIp" );
if ( null != ranges && 0 < ranges.length)
perm.addIpRange( ranges[0] );
else break;
mCount++;
} while( true );
// -> list: IpPermissions.n.Groups.m.UserId and IpPermissions.n.Groups.m.GroupName
mCount = 1;
do
{ String[] user = request.getParameterValues( "IpPermissions." + nCount + ".Groups." + mCount + ".UserId" );
if ( null == user || 0 == user.length) break;
String[] name = request.getParameterValues( "IpPermissions." + nCount + ".Groups." + mCount + ".GroupName" );
if ( null == name || 0 == name.length) break;
EC2SecurityGroup group = new EC2SecurityGroup();
group.setAccount( user[0] );
group.setName( name[0] );
perm.addUser( group );
mCount++;
} while( true );
// -> multiple IP permissions can be specified per group name
EC2request.addIpPermission( perm );
nCount++;
} while( true );
if (1 == nCount) {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - IpPermissions");
}
// -> execute the request
AuthorizeSecurityGroupIngressResponse EC2response = EC2SoapServiceImpl.toAuthorizeSecurityGroupIngressResponse(
ServiceProvider.getInstance().getEC2Engine().authorizeSecurityGroup( EC2request ));
serializeResponse(response, EC2response);
}
private void detachVolume( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
EC2Volume EC2request = new EC2Volume();
String[] volumeId = request.getParameterValues( "VolumeId" );
if ( null != volumeId && 0 < volumeId.length )
EC2request.setId(volumeId[0]);
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter 'VolumeId'");
}
String[] instanceId = request.getParameterValues( "InstanceId" );
if ( null != instanceId && 0 < instanceId.length )
EC2request.setInstanceId(instanceId[0]);
String[] device = request.getParameterValues( "Device" );
if ( null != device && 0 < device.length )
EC2request.setDevice( device[0] );
// -> execute the request
DetachVolumeResponse EC2response = EC2SoapServiceImpl.toDetachVolumeResponse( ServiceProvider.getInstance().getEC2Engine().detachVolume( EC2request ));
serializeResponse(response, EC2response);
}
private void deleteVolume( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
EC2Volume EC2request = new EC2Volume();
String[] volumeId = request.getParameterValues( "VolumeId" );
if ( null != volumeId && 0 < volumeId.length )
EC2request.setId(volumeId[0]);
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - VolumeId");
}
// -> execute the request
DeleteVolumeResponse EC2response = EC2SoapServiceImpl.toDeleteVolumeResponse( ServiceProvider.getInstance().getEC2Engine().deleteVolume( EC2request ));
serializeResponse(response, EC2response);
}
private void createVolume( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
EC2CreateVolume EC2request = new EC2CreateVolume();
String[] zoneName = request.getParameterValues( "AvailabilityZone" );
if ( null != zoneName && 0 < zoneName.length )
EC2request.setZoneName( zoneName[0] );
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing parameter - AvailabilityZone");
}
String[] size = request.getParameterValues( "Size" );
String[] snapshotId = request.getParameterValues("SnapshotId");
boolean useSnapshot = false;
boolean useSize = false;
if (null != size && 0 < size.length)
useSize = true;
if (snapshotId != null && snapshotId.length != 0)
useSnapshot = true;
if (useSize && !useSnapshot) {
EC2request.setSize( size[0] );
} else if (useSnapshot && !useSize) {
EC2request.setSnapshotId(snapshotId[0]);
} else if (useSize && useSnapshot) {
throw new EC2ServiceException( ClientError.InvalidParameterCombination, "Parameters 'Size' and 'SnapshotId' are mutually exclusive");
} else {
throw new EC2ServiceException( ClientError.MissingParamter, "Parameter 'Size' or 'SnapshotId' has to be specified");
}
// -> execute the request
CreateVolumeResponse EC2response = EC2SoapServiceImpl.toCreateVolumeResponse( ServiceProvider.getInstance().getEC2Engine().createVolume( EC2request ));
serializeResponse(response, EC2response);
}
private void createSecurityGroup( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
String groupName, groupDescription = null;
String[] name = request.getParameterValues( "GroupName" );
if ( null != name && 0 < name.length )
groupName = name[0];
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - GroupName");
}
String[] desc = request.getParameterValues( "GroupDescription" );
if ( null != desc && 0 < desc.length )
groupDescription = desc[0];
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - GroupDescription");
}
// -> execute the request
CreateSecurityGroupResponse EC2response = EC2SoapServiceImpl.toCreateSecurityGroupResponse( ServiceProvider.getInstance().getEC2Engine().createSecurityGroup( groupName, groupDescription ));
serializeResponse(response, EC2response);
}
private void deleteSecurityGroup( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
String groupName = null;
String[] name = request.getParameterValues( "GroupName" );
if ( null != name && 0 < name.length )
groupName = name[0];
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - GroupName");
}
// -> execute the request
DeleteSecurityGroupResponse EC2response = EC2SoapServiceImpl.toDeleteSecurityGroupResponse( ServiceProvider.getInstance().getEC2Engine().deleteSecurityGroup( groupName ));
serializeResponse(response, EC2response);
}
private void deleteSnapshot( HttpServletRequest request, HttpServletResponse response )
throws ADBException, XMLStreamException, IOException {
String snapshotId = null;
String[] snapSet = request.getParameterValues( "SnapshotId" );
if ( null != snapSet && 0 < snapSet.length )
snapshotId = snapSet[0];
else {
throw new EC2ServiceException( ClientError.MissingParamter, "Missing required parameter - SnapshotId");
}
// -> execute the request
DeleteSnapshotResponse EC2response = EC2SoapServiceImpl.toDeleteSnapshotResponse( ServiceProvider.getInstance().getEC2Engine().deleteSnapshot( snapshotId ));