2020import com .google .common .util .concurrent .ListenableFuture ;
2121import io .grpc .ConnectivityState ;
2222import io .grpc .ManagedChannel ;
23- import io .lighty .aaa .encrypt .service .impl .AAAEncryptionServiceImpl ;
2423import io .lighty .gnmi .southbound .device .connection .DeviceConnection ;
2524import io .lighty .gnmi .southbound .device .connection .DeviceConnectionInitializer ;
2625import io .lighty .gnmi .southbound .device .session .security .KeystoreGnmiSecurityProvider ;
3635import java .nio .charset .Charset ;
3736import java .nio .file .Files ;
3837import java .nio .file .Paths ;
39- import java .security .InvalidAlgorithmParameterException ;
40- import java .security .InvalidKeyException ;
41- import java .security .NoSuchAlgorithmException ;
42- import java .security .spec .InvalidKeySpecException ;
43- import java .security .spec .KeySpec ;
44- import java .util .Base64 ;
4538import java .util .Optional ;
4639import java .util .concurrent .ExecutorService ;
4740import java .util .concurrent .Executors ;
4841import java .util .concurrent .TimeUnit ;
49- import javax .crypto .NoSuchPaddingException ;
50- import javax .crypto .SecretKey ;
51- import javax .crypto .SecretKeyFactory ;
52- import javax .crypto .spec .GCMParameterSpec ;
53- import javax .crypto .spec .PBEKeySpec ;
54- import javax .crypto .spec .SecretKeySpec ;
42+ import javax .crypto .BadPaddingException ;
43+ import javax .crypto .IllegalBlockSizeException ;
5544import javax .xml .bind .DatatypeConverter ;
5645import org .junit .jupiter .api .AfterEach ;
5746import org .junit .jupiter .api .Assertions ;
6049import org .mockito .ArgumentCaptor ;
6150import org .mockito .MockitoAnnotations ;
6251import org .mockito .Spy ;
52+ import org .opendaylight .aaa .encrypt .impl .AAAEncryptionServiceImpl ;
6353import org .opendaylight .mdsal .binding .api .ReadTransaction ;
6454import org .opendaylight .mdsal .binding .api .WriteTransaction ;
6555import org .opendaylight .mdsal .binding .dom .adapter .BindingDOMDataBrokerAdapter ;
6656import org .opendaylight .mdsal .common .api .CommitInfo ;
6757import org .opendaylight .mdsal .common .api .LogicalDatastoreType ;
6858import org .opendaylight .yang .gen .v1 .config .aaa .authn .encrypt .service .config .rev240202 .AaaEncryptServiceConfig ;
6959import org .opendaylight .yang .gen .v1 .config .aaa .authn .encrypt .service .config .rev240202 .AaaEncryptServiceConfigBuilder ;
60+ import org .opendaylight .yang .gen .v1 .config .aaa .authn .encrypt .service .config .rev240202 .EncryptServiceConfig ;
7061import org .opendaylight .yang .gen .v1 .urn .ietf .params .xml .ns .yang .ietf .inet .types .rev130715 .Host ;
7162import org .opendaylight .yang .gen .v1 .urn .ietf .params .xml .ns .yang .ietf .inet .types .rev130715 .IpAddress ;
7263import org .opendaylight .yang .gen .v1 .urn .ietf .params .xml .ns .yang .ietf .inet .types .rev130715 .Ipv4Address ;
@@ -413,7 +404,7 @@ private static InstanceIdentifier<Keystore> getKeystore2Identifier() {
413404 .build ();
414405 }
415406
416- private static Keystore getKeystore1WithPassResponse () {
407+ private static Keystore getKeystore1WithPassResponse () throws IllegalBlockSizeException , BadPaddingException {
417408 return new KeystoreBuilder ()
418409 .setCaCertificate (getResource (CA_CRT ))
419410 .setClientCert (getResource (CLIENT_ENCRYPTED_CRT ))
@@ -426,7 +417,7 @@ private static Keystore getKeystore1WithPassResponse() {
426417 .build ();
427418 }
428419
429- private static Keystore getKeystore2Response () {
420+ private static Keystore getKeystore2Response () throws IllegalBlockSizeException , BadPaddingException {
430421 return new KeystoreBuilder ().setCaCertificate (getResource (CA_CRT )).setClientCert (getResource (CLIENT_CRT ))
431422 .setClientKey (DatatypeConverter .printBase64Binary (
432423 (AAA_ENCRYPTION_SERVICE .encrypt ((getResource (CLIENT_KEY ).getBytes ())))))
@@ -447,26 +438,25 @@ private static String getResource(String path) {
447438 }
448439
449440 private static AAAEncryptionServiceImpl createEncryptionServiceWithErrorHandling () {
450- try {
451- return createEncryptionService ();
452- } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeySpecException
453- | InvalidAlgorithmParameterException | InvalidKeyException e ) {
454- throw new RuntimeException ("Failed to create encryption service" , e );
455- }
441+ return createEncryptionService ();
456442 }
457443
458- private static AAAEncryptionServiceImpl createEncryptionService () throws NoSuchPaddingException ,
459- NoSuchAlgorithmException , InvalidKeySpecException , InvalidAlgorithmParameterException , InvalidKeyException {
460- final AaaEncryptServiceConfig encrySrvConfig = getDefaultAaaEncryptServiceConfig ();
461- final byte [] encryptionKeySalt = Base64 .getDecoder ().decode (encrySrvConfig .getEncryptSalt ());
462- final SecretKeyFactory keyFactory = SecretKeyFactory .getInstance (encrySrvConfig .getEncryptMethod ());
463- final KeySpec keySpec = new PBEKeySpec (encrySrvConfig .getEncryptKey ().toCharArray (), encryptionKeySalt ,
464- encrySrvConfig .getEncryptIterationCount (), encrySrvConfig .getEncryptKeyLength ());
465- final SecretKey key = new SecretKeySpec (keyFactory .generateSecret (keySpec ).getEncoded (),
466- encrySrvConfig .getEncryptType ());
467- final GCMParameterSpec ivParameterSpec = new GCMParameterSpec (encrySrvConfig .getAuthTagLength (),
468- encryptionKeySalt );
469- return new AAAEncryptionServiceImpl (ivParameterSpec , encrySrvConfig .getCipherTransforms (), key );
444+ private static AAAEncryptionServiceImpl createEncryptionService () {
445+ // Build configuration from your test constants
446+ AaaEncryptServiceConfig config = new AaaEncryptServiceConfigBuilder ()
447+ .setEncryptKey ("V1S1ED4OMeEh" )
448+ .setPasswordLength (12 )
449+ .setEncryptSalt ("TdtWeHbch/7xP52/rp3Usw==" )
450+ .setEncryptMethod ("PBKDF2WithHmacSHA1" )
451+ .setEncryptType ("AES" )
452+ .setEncryptIterationCount (32768 )
453+ .setEncryptKeyLength (128 )
454+ .setAuthTagLength (128 )
455+ .setCipherTransforms ("AES/GCM/NoPadding" )
456+ .build ();
457+
458+ // The ODL impl expects EncryptServiceConfig, so cast is fine
459+ return new AAAEncryptionServiceImpl ((EncryptServiceConfig ) config );
470460 }
471461
472462 private static AaaEncryptServiceConfig getDefaultAaaEncryptServiceConfig () {
0 commit comments