Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 3763c6d

Browse files
authored
fix: don't require username with x509 auth (#330)
1 parent e251875 commit 3763c6d

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

lib/model.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,15 @@ Connection = AmpersandModel.extend({
868868
throw new TypeError('sslCA is required when ssl is SERVER.');
869869
} else if (attrs.sslMethod === 'ALL') {
870870
if (!attrs.sslCA) {
871-
throw new TypeError('sslCA is required when ssl is ALL.');
871+
throw new TypeError('SSL \'Certificate Authority\' is required when the SSL method is set to \'Server and Client Validation\'.');
872872
}
873873

874-
if (!attrs.sslKey) {
875-
throw new TypeError('sslKey is required when ssl is ALL.');
874+
if (!attrs.sslCert) {
875+
throw new TypeError('SSL \'Client Certificate\' is required when the SSL method is set to \'Server and Client Validation\'.');
876876
}
877877

878-
if (!attrs.sslCert) {
879-
throw new TypeError('sslCert is required when ssl is ALL.');
878+
if (!attrs.sslKey) {
879+
throw new TypeError('SSL \'Client Private Key\' is required when the SSL method is set to \'Server and Client Validation\'.');
880880
}
881881
}
882882
},
@@ -887,15 +887,15 @@ Connection = AmpersandModel.extend({
887887
) {
888888
if (!attrs.mongodbUsername) {
889889
throw new TypeError(
890-
'The mongodbUsername field is required when ' +
891-
'using MONGODB or SCRAM-SHA-256 for authStrategy.'
890+
'The \'Username\' field is required when ' +
891+
'using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.'
892892
);
893893
}
894894

895895
if (!attrs.mongodbPassword) {
896896
throw new TypeError(
897-
'The mongodbPassword field is required when ' +
898-
'using MONGODB or SCRAM-SHA-256 for authStrategy.'
897+
'The \'Password\' field is required when ' +
898+
'using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.'
899899
);
900900
}
901901
}
@@ -909,41 +909,41 @@ Connection = AmpersandModel.extend({
909909
if (attrs.kerberosServiceName) {
910910
throw new TypeError(
911911
format(
912-
'The kerberosServiceName field does not apply when ' +
913-
'using %s for authStrategy.',
912+
'The Kerberos \'Service Name\' field does not apply when ' +
913+
'using %s for authentication.',
914914
attrs.authStrategy
915915
)
916916
);
917917
}
918918
if (attrs.kerberosPrincipal) {
919919
throw new TypeError(
920920
format(
921-
'The kerberosPrincipal field does not apply when ' +
922-
'using %s for authStrategy.',
921+
'The Kerberos \'Principal\' field does not apply when ' +
922+
'using %s for authentication.',
923923
attrs.authStrategy
924924
)
925925
);
926926
}
927927
if (attrs.kerberosPassword) {
928928
throw new TypeError(
929929
format(
930-
'The kerberosPassword field does not apply when ' +
931-
'using %s for authStrategy.',
930+
'The Kerberos \'Password\' field does not apply when ' +
931+
'using %s for authentication.',
932932
attrs.authStrategy
933933
)
934934
);
935935
}
936936
} else if (!attrs.kerberosPrincipal) {
937937
throw new TypeError(
938-
'The kerberosPrincipal field is required when using KERBEROS for authStrategy.'
938+
'The Kerberos \'Principal\' field is required when using \'Kerberos\' for authentication.'
939939
);
940940
}
941941
},
942942
validateX509(attrs) {
943943
if (attrs.authStrategy === 'X509') {
944-
if (!attrs.x509Username) {
944+
if (attrs.sslMethod !== 'ALL') {
945945
throw new TypeError(
946-
'The x509Username field is required when using X509 for authStrategy.'
946+
'SSL method is required to be set to \'Server and Client Validation\' when using X.509 authentication.'
947947
);
948948
}
949949
}
@@ -953,16 +953,16 @@ Connection = AmpersandModel.extend({
953953
if (!attrs.ldapUsername) {
954954
throw new TypeError(
955955
format(
956-
'The ldapUsername field is required when ' +
957-
'using LDAP for authStrategy.'
956+
'The \'Username\' field is required when ' +
957+
'using \'LDAP\' for authentication.'
958958
)
959959
);
960960
}
961961
if (!attrs.ldapPassword) {
962962
throw new TypeError(
963963
format(
964-
'The ldapPassword field is required when ' +
965-
'using LDAP for authStrategy.'
964+
'The \'Password\' field is required when ' +
965+
'using LDAP for authentication.'
966966
)
967967
);
968968
}
@@ -978,35 +978,35 @@ Connection = AmpersandModel.extend({
978978

979979
if (!attrs.sshTunnelPassword) {
980980
throw new TypeError(
981-
'sslTunnelPassword is required when sshTunnel is USER_PASSWORD.'
981+
'\'SSH Password\' is required when SSH Tunnel is set to \'Use Password\'.'
982982
);
983983
}
984984
} else if (attrs.sshTunnel === 'IDENTITY_FILE') {
985985
this.validateStandardSshTunnelOptions(attrs);
986986

987987
if (!attrs.sshTunnelIdentityFile) {
988988
throw new TypeError(
989-
'sslTunnelIdentityFile is required when sshTunnel is IDENTITY_FILE.'
989+
'\'SSH Identity File\' is required when SSH Tunnel is set to \'Use Identity File\'.'
990990
);
991991
}
992992
}
993993
},
994994
validateStandardSshTunnelOptions(attrs) {
995995
if (!attrs.sshTunnelUsername) {
996996
throw new TypeError(
997-
'sslTunnelUsername is required when sshTunnel is not NONE.'
997+
'\'SSH Username\' is required when SSH Tunnel is set.'
998998
);
999999
}
10001000

10011001
if (!attrs.sshTunnelHostname) {
10021002
throw new TypeError(
1003-
'sslTunnelHostname is required when sshTunnel is not NONE.'
1003+
'\'SSH Hostname\' is required when SSH Tunnel is set.'
10041004
);
10051005
}
10061006

10071007
if (!attrs.sshTunnelPort) {
10081008
throw new TypeError(
1009-
'sslTunnelPort is required when sshTunnel is not NONE.'
1009+
'\'SSH Tunnel Port\' is required when SSH Tunnel is set.'
10101010
);
10111011
}
10121012
},

test/build-uri.test.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ describe('Connection model builder', () => {
594594
const error = c.validate(attrs);
595595

596596
expect(c.isValid()).to.be.equal(false);
597-
expect(error.message).to.include('mongodbUsername field is required');
597+
expect(error.message).to.include('The \'Username\' field is required when using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.');
598598
});
599599

600600
it('should throw the error if auth is SCRAM-SHA-256 and mongodbPassword is missing', () => {
@@ -606,7 +606,7 @@ describe('Connection model builder', () => {
606606
const error = c.validate(attrs);
607607

608608
expect(c.isValid()).to.be.equal(false);
609-
expect(error.message).to.include('mongodbPassword field is required');
609+
expect(error.message).to.equal('The \'Password\' field is required when using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.');
610610
});
611611

612612
it('should throw the error if MONGODB auth receives non-applicable fields', () => {
@@ -619,8 +619,8 @@ describe('Connection model builder', () => {
619619
const error = c.validate(attrs);
620620

621621
expect(c.isValid()).to.be.equal(false);
622-
expect(error.message).to.include(
623-
'kerberosServiceName field does not apply'
622+
expect(error.message).to.equal(
623+
'The Kerberos \'Service Name\' field does not apply when using MONGODB for authentication.'
624624
);
625625
});
626626

@@ -683,7 +683,7 @@ describe('Connection model builder', () => {
683683
const error = c.validate(attrs);
684684

685685
expect(c.isValid()).to.be.equal(false);
686-
expect(error.message).to.include('mongodbUsername field is required');
686+
expect(error.message).to.include('The \'Username\' field is required when using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.');
687687
});
688688

689689
it('should throw the error if auth is MONGODB and mongodbPassword is missing', (done) => {
@@ -734,7 +734,7 @@ describe('Connection model builder', () => {
734734
const error = c.validate(attrs);
735735

736736
expect(c.isValid()).to.be.equal(false);
737-
expect(error.message).to.include('ldapUsername field is required');
737+
expect(error.message).to.equal('The \'Username\' field is required when using \'LDAP\' for authentication.');
738738
});
739739

740740
it('should throw the error if auth is LDAP and ldapPassword is missing', () => {
@@ -743,7 +743,7 @@ describe('Connection model builder', () => {
743743
const error = c.validate(attrs);
744744

745745
expect(c.isValid()).to.be.equal(false);
746-
expect(error.message).to.include('ldapPassword field is required');
746+
expect(error.message).to.equal('The \'Password\' field is required when using LDAP for authentication.');
747747
});
748748

749749
it('should set authStrategy to X509', (done) => {
@@ -760,13 +760,28 @@ describe('Connection model builder', () => {
760760
});
761761
});
762762

763-
it('should throw the error if auth is X509 and x509Username is missing', () => {
764-
const attrs = { authStrategy: 'X509' };
763+
it('should not throw the error if auth is X509 and x509Username is missing', () => {
764+
const attrs = {
765+
authStrategy: 'X509',
766+
sslMethod: 'ALL',
767+
sslCA: [fixture.ssl.ca],
768+
sslCert: fixture.ssl.server,
769+
sslKey: fixture.ssl.server
770+
};
771+
const c = new Connection(attrs);
772+
773+
expect(c.isValid()).to.be.equal(true);
774+
});
775+
776+
it('should throw a validation error if auth is X509 and sslMethod is not "ALL"', () => {
777+
const attrs = {
778+
authStrategy: 'X509'
779+
};
765780
const c = new Connection(attrs);
766781
const error = c.validate(attrs);
767782

768783
expect(c.isValid()).to.be.equal(false);
769-
expect(error.message).to.include('x509Username field is required');
784+
expect(error.message).to.equal('SSL method is required to be set to \'Server and Client Validation\' when using X.509 authentication.');
770785
});
771786

772787
it('should set default mongodb gssapiServiceName when using KERBEROS auth', (done) => {
@@ -803,7 +818,7 @@ describe('Connection model builder', () => {
803818
const error = c.validate(attrs);
804819

805820
expect(c.isValid()).to.be.equal(false);
806-
expect(error.message).to.include('kerberosPrincipal field is required');
821+
expect(error.message).to.equal('The Kerberos \'Principal\' field is required when using \'Kerberos\' for authentication.');
807822
});
808823

809824
it('should *only* require a kerberosPrincipal', () => {
@@ -936,7 +951,7 @@ describe('Connection model builder', () => {
936951
const error = c.validate(attrs);
937952

938953
expect(c.isValid()).to.be.equal(false);
939-
expect(error.message).to.include('mongodbPassword field is required');
954+
expect(error.message).to.equal('The \'Password\' field is required when using \'Username/Password\' or \'SCRAM-SHA-256\' for authentication.');
940955
});
941956

942957
it('should generate the local port when using a ssh tunne and bind to local port does not exist', () => {

0 commit comments

Comments
 (0)