11import logging
2+ import requests
3+ from requests .exceptions import HTTPError
24from portal .apps .onboarding .steps .abstract import AbstractStep
35from portal .apps .onboarding .state import SetupState
6+ from django .conf import settings
7+ from portal .utils .encryption import createKeyPair
48from portal .libs .agave .utils import service_account
59from tapipy .errors import BaseTapyException
6- from portal . utils . encryption import createKeyPair
10+
711
812logger = logging .getLogger (__name__ )
913
1014
11- def create_system_credentials_with_keys (client ,
12- username ,
13- public_key ,
14- private_key ,
15- system_id ,
16- skipCredentialCheck = False ) -> int :
15+ def create_system_credentials (client ,
16+ username ,
17+ public_key ,
18+ private_key ,
19+ system_id ,
20+ skipCredentialCheck = False ) -> int :
1721 """
1822 Set an RSA key pair as the user's auth credential on a Tapis system.
1923 """
20- logger .info (f"Creating user credential for { username } on Tapis system { system_id } using keys " )
24+ logger .info (f"Creating user credential for { username } on Tapis system { system_id } " )
2125 data = {'privateKey' : private_key , 'publicKey' : public_key }
2226 client .systems .createUserCredential (
2327 systemId = system_id ,
@@ -27,21 +31,16 @@ def create_system_credentials_with_keys(client,
2731 )
2832
2933
30- def create_system_credentials (client ,
31- username ,
32- system_id ,
33- createTmsKeys ,
34- skipCredentialCheck = False ) -> int :
34+ def register_public_key (username , publicKey , system_id ) -> int :
3535 """
36- Create user's auth credential on a Tapis system. This Tapis API uses TMS .
36+ Push a public key to the Key Service API .
3737 """
38- logger .info (f"Creating user credential for { username } on Tapis system { system_id } using TMS" )
39- client .systems .createUserCredential (
40- systemId = system_id ,
41- userName = username ,
42- createTmsKeys = createTmsKeys ,
43- skipCredentialCheck = skipCredentialCheck ,
44- )
38+ url = "https://api.tacc.utexas.edu/keys/v2/" + username
39+ headers = {"Authorization" : "Bearer {}" .format (settings .KEY_SERVICE_TOKEN )}
40+ data = {"key_value" : publicKey , "tags" : [{"name" : "system" , "value" : system_id }]}
41+ response = requests .post (url , json = data , headers = headers )
42+ response .raise_for_status ()
43+ return response .status_code
4544
4645
4746def set_user_permissions (user , system_id ):
@@ -78,12 +77,6 @@ def prepare(self):
7877 self .state = SetupState .PENDING
7978 self .log ("Awaiting TACC systems access." )
8079
81- def get_system (self , system_id ) -> None :
82- """
83- Get the system definition
84- """
85- return self .user .tapis_oauth .client .systems .getSystem (systemId = system_id )
86-
8780 def check_system (self , system_id ) -> None :
8881 """
8982 Check whether a user already has access to a storage system by attempting a listing.
@@ -108,17 +101,21 @@ def process(self):
108101 except BaseTapyException :
109102 self .log (f"Creating credentials for system: { system } " )
110103
111- system_definition = self .get_system (system )
104+ (priv , pub ) = createKeyPair ()
105+
106+ try :
107+ register_public_key (self .user .username , pub , system )
108+ self .log (f"Successfully registered public key for system: { system } " )
109+ except HTTPError as e :
110+ logger .error (e )
111+ self .fail (f"Failed to register public key with key service for system: { system } " )
112+
112113 try :
113- if system_definition .get ("defaultAuthnMethod" ) != 'TMS_KEYS' :
114- (priv , pub ) = createKeyPair ()
115- create_system_credentials_with_keys (
116- self .user .tapis_oauth .client , self .user .username , pub , priv , system
117- )
118- else :
119- create_system_credentials (
120- self .user .tapis_oauth .client , self .user .username , system , createTmsKeys = True
121- )
114+ create_system_credentials (self .user .tapis_oauth .client ,
115+ self .user .username ,
116+ pub ,
117+ priv ,
118+ system )
122119 self .log (f"Successfully created credentials for system: { system } " )
123120 except BaseTapyException as e :
124121 logger .error (e )
0 commit comments