Skip to content

Commit 18cd839

Browse files
committed
Prompt for passphrase if it's necessary
Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
1 parent 3c95f6d commit 18cd839

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

doc/install.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ contains the code ``NotAuthorizedOrNotFound``, then there's an alternative way
5555
to setup your OCI credentials:
5656

5757
1. Run ``oci setup keys``. At the prompt, you probably want to type "N/A" to
58-
avoid setting a passphrase for the key.
58+
avoid setting a passphrase for the key. However, if you really feel it is
59+
necessary, you can use a passphrase. Yo will detect that it is necessary and
60+
prompt you for it.
5961
2. Open the OCI web console. Navigate to your profile using the icon at the top
6062
right (or search your email address in the search bar). On your profile page,
6163
select the "API keys" link, and then choose "Add API key".

yo/api.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)