6363import org .bouncycastle .openssl .PEMReader ;
6464import org .bouncycastle .openssl .PasswordFinder ;
6565
66+ import com .cloud .domain .dao .DomainDao ;
67+ import com .cloud .domain .DomainVO ;
6668import com .cloud .event .ActionEvent ;
6769import com .cloud .event .EventTypes ;
6870import com .cloud .exception .InvalidParameterValueException ;
7375import com .cloud .network .dao .SslCertVO ;
7476import com .cloud .network .lb .CertService ;
7577import com .cloud .network .rules .LoadBalancer ;
78+ import com .cloud .projects .Project ;
79+ import com .cloud .projects .ProjectService ;
7680import com .cloud .user .Account ;
7781import com .cloud .user .AccountManager ;
7882import com .cloud .user .dao .AccountDao ;
@@ -90,6 +94,10 @@ public class CertServiceImpl implements CertService {
9094 @ Inject
9195 AccountDao _accountDao ;
9296 @ Inject
97+ ProjectService _projectMgr ;
98+ @ Inject
99+ DomainDao _domainDao ;
100+ @ Inject
93101 SslCertDao _sslCertDao ;
94102 @ Inject
95103 LoadBalancerCertMapDao _lbCertDao ;
@@ -105,7 +113,6 @@ public CertServiceImpl() {
105113 @ ActionEvent (eventType = EventTypes .EVENT_LB_CERT_UPLOAD , eventDescription = "Uploading a certificate to cloudstack" , async = false )
106114 public SslCertResponse uploadSslCert (UploadSslCertCmd certCmd ) {
107115 try {
108-
109116 String cert = certCmd .getCert ();
110117 String key = certCmd .getKey ();
111118 String password = certCmd .getPassword ();
@@ -116,8 +123,18 @@ public SslCertResponse uploadSslCert(UploadSslCertCmd certCmd) {
116123
117124 String fingerPrint = generateFingerPrint (parseCertificate (cert ));
118125
119- Long accountId = CallContext .current ().getCallingAccount ().getId ();
120- Long domainId = CallContext .current ().getCallingAccount ().getDomainId ();
126+ CallContext ctx = CallContext .current ();
127+ Account caller = ctx .getCallingAccount ();
128+
129+ Account owner = null ;
130+ if ((certCmd .getAccountName () != null && certCmd .getDomainId () != null ) || certCmd .getProjectId () != null ) {
131+ owner = _accountMgr .finalizeOwner (caller , certCmd .getAccountName (), certCmd .getDomainId (), certCmd .getProjectId ());
132+ } else {
133+ owner = caller ;
134+ }
135+
136+ Long accountId = owner .getId ();
137+ Long domainId = owner .getDomainId ();
121138
122139 SslCertVO certVO = new SslCertVO (cert , key , password , chain , accountId , domainId , fingerPrint );
123140 _sslCertDao .persist (certVO );
@@ -170,18 +187,18 @@ public List<SslCertResponse> listSslCerts(ListSslCertsCmd listSslCertCmd) {
170187 Long certId = listSslCertCmd .getCertId ();
171188 Long accountId = listSslCertCmd .getAccountId ();
172189 Long lbRuleId = listSslCertCmd .getLbId ();
190+ Long projectId = listSslCertCmd .getProjectId ();
173191
174192 List <SslCertResponse > certResponseList = new ArrayList <SslCertResponse >();
175193
176- if (certId == null && accountId == null && lbRuleId == null ) {
177- throw new InvalidParameterValueException ("Invalid parameters either certificate ID or Account ID or Loadbalancer ID required" );
194+ if (certId == null && accountId == null && lbRuleId == null && projectId == null ) {
195+ throw new InvalidParameterValueException ("Invalid parameters either certificate ID or Account ID or Loadbalancer ID or Project ID required" );
178196 }
179197
180198 List <LoadBalancerCertMapVO > certLbMap = null ;
181199 SslCertVO certVO = null ;
182200
183201 if (certId != null ) {
184-
185202 certVO = _sslCertDao .findById (certId );
186203
187204 if (certVO == null ) {
@@ -200,7 +217,7 @@ public List<SslCertResponse> listSslCerts(ListSslCertsCmd listSslCertCmd) {
200217 LoadBalancer lb = _entityMgr .findById (LoadBalancerVO .class , lbRuleId );
201218
202219 if (lb == null ) {
203- throw new InvalidParameterValueException ("found no loadbalancer wth id: " + lbRuleId );
220+ throw new InvalidParameterValueException ("Found no loadbalancer with id: " + lbRuleId );
204221 }
205222
206223 _accountMgr .checkAccess (caller , SecurityChecker .AccessType .UseEntry , true , lb );
@@ -222,6 +239,25 @@ public List<SslCertResponse> listSslCerts(ListSslCertsCmd listSslCertCmd) {
222239
223240 }
224241
242+ if (projectId != null ) {
243+ Project project = _projectMgr .getProject (projectId );
244+
245+ if (project == null ) {
246+ throw new InvalidParameterValueException ("Found no project with id: " + projectId );
247+ }
248+
249+ List <SslCertVO > projectCertVOList = _sslCertDao .listByAccountId (project .getProjectAccountId ());
250+ if (projectCertVOList == null || projectCertVOList .isEmpty ())
251+ return certResponseList ;
252+ _accountMgr .checkAccess (caller , SecurityChecker .AccessType .UseEntry , true , projectCertVOList .get (0 ));
253+
254+ for (SslCertVO cert : projectCertVOList ) {
255+ certLbMap = _lbCertDao .listByCertId (cert .getId ());
256+ certResponseList .add (createCertResponse (cert , certLbMap ));
257+ }
258+ return certResponseList ;
259+ }
260+
225261 //reached here look by accountId
226262 List <SslCertVO > certVOList = _sslCertDao .listByAccountId (accountId );
227263 if (certVOList == null || certVOList .isEmpty ())
@@ -232,7 +268,6 @@ public List<SslCertResponse> listSslCerts(ListSslCertsCmd listSslCertCmd) {
232268 certLbMap = _lbCertDao .listByCertId (cert .getId ());
233269 certResponseList .add (createCertResponse (cert , certLbMap ));
234270 }
235-
236271 return certResponseList ;
237272 }
238273
@@ -264,13 +299,24 @@ public SslCertResponse createCertResponse(SslCertVO cert, List<LoadBalancerCertM
264299 SslCertResponse response = new SslCertResponse ();
265300
266301 Account account = _accountDao .findByIdIncludingRemoved (cert .getAccountId ());
302+ if (account .getType () == Account .ACCOUNT_TYPE_PROJECT ) {
303+ // find the project
304+ Project project = _projectMgr .findByProjectAccountIdIncludingRemoved (account .getId ());
305+ response .setProjectId (project .getUuid ());
306+ response .setProjectName (project .getName ());
307+ } else {
308+ response .setAccountName (account .getAccountName ());
309+ }
310+
311+ DomainVO domain = _domainDao .findByIdIncludingRemoved (cert .getDomainId ());
312+ response .setDomainId (domain .getUuid ());
313+ response .setDomainName (domain .getName ());
267314
268315 response .setObjectName ("sslcert" );
269316 response .setId (cert .getUuid ());
270317 response .setCertificate (cert .getCertificate ());
271318 response .setPrivatekey (cert .getKey ());
272319 response .setFingerprint (cert .getFingerPrint ());
273- response .setAccountName (account .getAccountName ());
274320
275321 if (cert .getChain () != null )
276322 response .setCertchain (cert .getChain ());
0 commit comments