@@ -1033,7 +1033,40 @@ def _setup_oci(self) -> None:
10331033 # a value provided from its own config, to allow users to update the
10341034 # region, VCN ID, and AD configuration, all in one place.
10351035 self ._oci_cfg ["region" ] = self .config .region
1036- self ._vnet = foo .oci .core .VirtualNetworkClient (self ._oci_cfg )
1036+
1037+ # First try to load a client without using a passphrase. If that fails,
1038+ # we need to prompt and retry with a passphrase. If that fails, the
1039+ # passphrase was likely wrong, so we continue until we succeed in
1040+ # creating the client, or until we get any other error.
1041+ #
1042+ # KNOWN BUG: Some versions of cryptography / OpenSSL (the built-in ones
1043+ # on OL9, at least) will give a spurious prompt in case you provide the
1044+ # wrong password. This doesn't happen with recent versions from pip, and
1045+ # it can't be sanely monkey-patched. As a result, we just need to live
1046+ # with the double prompt. Try to get the password right on the first try.
1047+ # See: https://github.com/oracle/oci-python-sdk/issues/697
1048+ while True :
1049+ try :
1050+ self ._vnet = foo .oci .core .VirtualNetworkClient (self ._oci_cfg )
1051+ except foo .oci .exceptions .MissingPrivateKeyPassphrase :
1052+ needs_passphrase = True
1053+ self ._oci_cfg ["pass_phrase" ] = self .con .input (
1054+ prompt = "OCI API Key Passphrase: " ,
1055+ password = True ,
1056+ )
1057+ except foo .oci .exceptions .InvalidPrivateKey :
1058+ # The error is vague: either the password is wrong, or it's the
1059+ # wrong type of key. We can be more specific, since we know
1060+ # whether a password is required, we already tried without it.
1061+ if not needs_passphrase :
1062+ raise
1063+ self .con .print ("[red]Incorrect passphrase[/red]" )
1064+ self ._oci_cfg ["pass_phrase" ] = self .con .input (
1065+ prompt = "OCI API Key Passphrase: " ,
1066+ password = True ,
1067+ )
1068+ else :
1069+ break
10371070 self ._compute = foo .oci .core .ComputeClient (self ._oci_cfg )
10381071 self ._block = foo .oci .core .BlockstorageClient (self ._oci_cfg )
10391072 self ._iam = foo .oci .identity .IdentityClient (self ._oci_cfg )
0 commit comments