Skip to content

Commit bbce239

Browse files
fix: Fixed to match the current PR endpoint
1 parent ba9d578 commit bbce239

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

Pilot/pilotCommands.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,19 @@ def __init__(self, pilotParams):
593593
def execute(self):
594594
"""Calls diracX api"""
595595

596-
if not self.pp.pilotReference:
597-
self.log.warn("Skipping module, no pilot reference found")
598-
return
596+
if not self.pp.pilotUUID:
597+
self.log.error("PilotUUID not given, exiting...")
598+
sys.exit(-1)
599599

600600
if not self.pp.pilotSecret:
601-
self.log.warn("Skipping module, no pilot secret found")
602-
return
601+
self.log.error("PilotSecret not given, exiting...")
602+
sys.exit(-1)
603+
604+
if not self.pp.diracXServer:
605+
self.log.error("DiracXServer (url) not given, exiting...")
606+
sys.exit(-1)
607+
608+
self.log.info("Fetching JWT in DiracX (URL: %s)" % self.pp.diracXServer)
603609

604610
config = BaseRequest(
605611
"%s/api/auth/pilot-login" % (
@@ -610,11 +616,16 @@ def execute(self):
610616

611617
config.generateUserAgent(self.pp.pilotUUID)
612618

613-
self.pp.jwt = config.executeRequest({
614-
"pilot_stamp": self.pp.pilotStamp,
615-
"pilot_secret": self.pp.pilotSecret
616-
}, insecure=True)
617-
619+
try:
620+
self.pp.jwt = config.executeRequest({
621+
"pilot_stamp": self.pp.pilotUUID,
622+
"pilot_secret": self.pp.pilotSecret
623+
}, insecure=True)
624+
except HTTPError as e:
625+
self.log.error("Request failed: %s" % str(e))
626+
627+
self.log.info("Fetched the pilot token with the pilot secret.")
628+
618629
class CheckCECapabilities(CommandBase):
619630
"""Used to get CE tags and other relevant parameters."""
620631

Pilot/pilotTools.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -729,26 +729,11 @@ def sendMessage(url, pilotUUID, wnVO, method, rawMessage, jwt={}):
729729
config.generateUserAgent(pilotUUID=pilotUUID)
730730

731731
# Do the request
732-
_res = config.executeRequest(raw_data=raw_data)
733-
734-
735-
736-
def retrieveJWT(diracXURL, pilotUUID, pilotSecret):
737-
738-
caPath = os.getenv("X509_CERT_DIR")
739-
740-
data_in_url = "?pilot_job_reference=%s&pilot_secret=%s" % (pilotUUID, pilotSecret)
741-
742-
config = BaseRequest(
743-
"%s/api/auth/pilot-login%s" % (diracXURL, data_in_url),
744-
caPath=caPath
732+
_res = config.executeRequest(
733+
raw_data=raw_data,
734+
content_type="x-www-form-urlencoded"
745735
)
746-
747-
config.addHeader("Content-Type", "application/json")
748-
749-
config.generateUserAgent(pilotUUID=pilotUUID)
750736

751-
return config.executeRequest(raw_data={}, insecure=True)
752737

753738
class CommandBase(object):
754739
"""CommandBase is the base class for every command in the pilot commands toolbox"""

Pilot/proxyTools.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
try:
1919
from urllib.parse import urlencode
20+
from urllib.error import HTTPError
2021
from urllib.request import Request, urlopen
2122
except ImportError:
2223
from urllib import urlencode
2324

24-
from urllib2 import Request, urlopen
25-
25+
from urllib2 import Request, urlopen, HTTPError
2626

2727
VOMS_FQANS_OID = b"1.3.6.1.4.1.8005.100.100.4"
2828
VOMS_EXTENSION_OID = b"1.3.6.1.4.1.8005.100.100.5"
@@ -81,7 +81,7 @@ def getVO(proxy_data):
8181

8282

8383
class BaseRequest(object):
84-
"""This class helps supporting multiple kinds of requests that requires connections"""
84+
"""This class helps supporting multiple kinds of requests that require connections"""
8585

8686
def __init__(self, url, caPath, name="unknown"):
8787
self.name = name
@@ -114,21 +114,32 @@ def addHeader(self, key, value):
114114
"""Add a header (key, value) into the request header"""
115115
self.headers[key] = value
116116

117-
def executeRequest(self, raw_data, insecure=False):
117+
def executeRequest(self, raw_data, insecure=False, content_type="json"):
118118
"""Execute a HTTP request with the data, headers, and the pre-defined data (SSL + auth)
119119
120120
:param raw_data: Data to send
121121
:type raw_data: dict
122122
:param insecure: Deactivate proxy verification WARNING Debug ONLY
123123
:type insecure: bool
124+
:param content_type: Data format to send, either "json" or "x-www-form-urlencoded"
125+
:type content_type: str
124126
:return: Parsed JSON response
125127
:rtype: dict
126-
"""
127-
if sys.version_info.major == 3:
128-
data = urlencode(raw_data).encode("utf-8") # encode to bytes ! for python3
128+
"""
129+
if content_type == "json":
130+
data = json.dumps(raw_data).encode("utf-8")
131+
self.headers["Content-Type"] = "application/json"
132+
elif content_type == "x-www-form-urlencoded":
133+
if sys.version_info.major == 3:
134+
data = urlencode(raw_data).encode("utf-8") # encode to bytes ! for python3
135+
else:
136+
# Python2
137+
data = urlencode(raw_data)
138+
self.headers["Content-Type"] = "application/x-www-form-urlencoded"
129139
else:
130-
# Python2
131-
data = urlencode(raw_data)
140+
raise ValueError("Invalid content_type. Use 'json' or 'x-www-form-urlencoded'.")
141+
142+
self.headers["Content-Length"] = str(len(data))
132143

133144
request = Request(self.url, data=data, headers=self.headers, method="POST")
134145

@@ -190,8 +201,12 @@ def __init__(self, url, caPath, certEnv):
190201
)
191202
self._hasExtraCredentials = True
192203

193-
def executeRequest(self, raw_data):
204+
def executeRequest(self, raw_data, insecure=False, content_type="json"):
194205
# Adds a flag if the passed cert is a Directory
195206
if self._hasExtraCredentials:
196207
raw_data["extraCredentials"] = '"hosts"'
197-
return super(X509BasedRequest, self).executeRequest(raw_data)
208+
return super(X509BasedRequest, self).executeRequest(
209+
raw_data,
210+
insecure=insecure,
211+
content_type=content_type
212+
)

0 commit comments

Comments
 (0)