Skip to content

Commit 6bec698

Browse files
committed
CLOUDSTACK-8037: Require signed AuthnRequest, adds more security
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 2ed76b2 commit 6bec698

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎plugins/user-authenticators/saml2/src/org/apache/cloudstack/api/command/GetServiceProviderMetaDataCmd.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
121121

122122
SPSSODescriptor spSSODescriptor = new SPSSODescriptorBuilder().buildObject();
123123
spSSODescriptor.setWantAssertionsSigned(true);
124-
spSSODescriptor.setAuthnRequestsSigned(false);
124+
spSSODescriptor.setAuthnRequestsSigned(true);
125125

126126
X509KeyInfoGeneratorFactory keyInfoGeneratorFactory = new X509KeyInfoGeneratorFactory();
127127
keyInfoGeneratorFactory.setEmitEntityCertificate(true);
@@ -134,7 +134,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
134134
signKeyDescriptor.setUse(UsageType.SIGNING);
135135

136136
BasicX509Credential credential = new BasicX509Credential();
137-
credential.setEntityCertificate(_samlAuthManager.getSpX509Key());
137+
credential.setEntityCertificate(_samlAuthManager.getSpX509Certificate());
138138
try {
139139
encKeyDescriptor.setKeyInfo(keyInfoGenerator.generate(credential));
140140
signKeyDescriptor.setKeyInfo(keyInfoGenerator.generate(credential));
@@ -148,6 +148,14 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
148148
nameIDFormat.setFormat(NameIDType.PERSISTENT);
149149
spSSODescriptor.getNameIDFormats().add(nameIDFormat);
150150

151+
NameIDFormat emailNameIDFormat = new NameIDFormatBuilder().buildObject();
152+
emailNameIDFormat.setFormat(NameIDType.EMAIL);
153+
spSSODescriptor.getNameIDFormats().add(emailNameIDFormat);
154+
155+
NameIDFormat transientNameIDFormat = new NameIDFormatBuilder().buildObject();
156+
transientNameIDFormat.setFormat(NameIDType.TRANSIENT);
157+
spSSODescriptor.getNameIDFormats().add(transientNameIDFormat);
158+
151159
AssertionConsumerService assertionConsumerService = new AssertionConsumerServiceBuilder().buildObject();
152160
assertionConsumerService.setIndex(0);
153161
assertionConsumerService.setBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI);

‎plugins/user-authenticators/saml2/src/org/apache/cloudstack/saml/SAML2AuthManager.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface SAML2AuthManager extends PluggableAPIAuthenticator {
2828

2929
public X509Certificate getIdpSigningKey();
3030
public X509Certificate getIdpEncryptionKey();
31-
public X509Certificate getSpX509Key();
31+
public X509Certificate getSpX509Certificate();
3232
public KeyPair getSpKeyPair();
3333

3434
public String getSpSingleSignOnUrl();

‎plugins/user-authenticators/saml2/src/org/apache/cloudstack/saml/SAML2AuthManagerImpl.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ private boolean setup() {
202202
s_logger.error("SAML2 Authentication may be unavailable");
203203
} catch (ConfigurationException | FactoryConfigurationError e) {
204204
s_logger.error("OpenSAML bootstrapping failed: error: " + e.getMessage());
205+
} catch (NullPointerException e) {
206+
s_logger.error("Unable to setup SAML Auth Plugin due to NullPointerException" +
207+
" please check the SAML IDP metadata URL and entity ID in global settings: " + e.getMessage());
205208
}
206209

207210
if (this.idpSingleLogOutUrl == null || this.idpSingleSignOnUrl == null) {
@@ -259,7 +262,7 @@ public Boolean isSAMLPluginEnabled() {
259262
return Boolean.valueOf(_configDao.getValue(Config.SAMLIsPluginEnabled.key()));
260263
}
261264

262-
public X509Certificate getSpX509Key() {
265+
public X509Certificate getSpX509Certificate() {
263266
return spX509Key;
264267
}
265268

‎utils/src/org/apache/cloudstack/utils/auth/SAMLUtils.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static AuthnRequest buildAuthnRequestObject(String spId, String idpUrl, S
148148
RequestedAuthnContextBuilder requestedAuthnContextBuilder = new RequestedAuthnContextBuilder();
149149
RequestedAuthnContext requestedAuthnContext = requestedAuthnContextBuilder.buildObject();
150150
requestedAuthnContext
151-
.setComparison(AuthnContextComparisonTypeEnumeration.MINIMUM);
151+
.setComparison(AuthnContextComparisonTypeEnumeration.EXACT);
152152
requestedAuthnContext.getAuthnContextClassRefs().add(
153153
authnContextClassRef);
154154

@@ -166,7 +166,7 @@ public static AuthnRequest buildAuthnRequestObject(String spId, String idpUrl, S
166166
authnRequest.setAssertionConsumerServiceURL(consumerUrl);
167167
authnRequest.setProviderName(spId);
168168
authnRequest.setNameIDPolicy(nameIdPolicy);
169-
//authnRequest.setRequestedAuthnContext(requestedAuthnContext);
169+
authnRequest.setRequestedAuthnContext(requestedAuthnContext);
170170

171171
return authnRequest;
172172
}
@@ -315,8 +315,8 @@ public static KeyPair generateRandomKeyPair() throws NoSuchProviderException, No
315315

316316
public static X509Certificate generateRandomX509Certificate(KeyPair keyPair) throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, SignatureException, InvalidKeyException {
317317
Date validityBeginDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
318-
Date validityEndDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);
319-
X500Principal dnName = new X500Principal("CN=Apache CloudStack");
318+
Date validityEndDate = new Date(System.currentTimeMillis() + 3 * 365 * 24 * 60 * 60 * 1000);
319+
X500Principal dnName = new X500Principal("CN=ApacheCloudStack");
320320
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
321321
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
322322
certGen.setSubjectDN(dnName);

0 commit comments

Comments
 (0)