Skip to content

Commit 8e52103

Browse files
committed
Fix pet peeve
1 parent e6f2a42 commit 8e52103

2 files changed

Lines changed: 104 additions & 103 deletions

File tree

registry/registry.go

Lines changed: 102 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -277,126 +277,127 @@ func keySignChallengeCommit(ctx *gin.Context, data *registrationData) (bool, map
277277
serverPubkey := serverPrivateKey.PublicKey
278278
serverVerified := verifySignature(serverPayload, serverSignature, &serverPubkey)
279279

280-
if clientVerified && serverVerified {
281-
// Ensure the user input prefix doesn't contain any invalid characters.
282-
// Also remove the trailing slash if it exists.
283-
reqPrefix, err := validatePrefix(data.Prefix)
284-
if err != nil {
285-
err = errors.Wrapf(err, "Requested namespace %s failed validation", data.Prefix)
286-
log.Errorln(err)
287-
return false, nil, badRequestError{Message: err.Error()}
288-
}
289-
data.Prefix = reqPrefix
280+
if !(clientVerified && serverVerified) {
281+
return false, nil, errors.Errorf("Unable to verify the client's public key, or an encountered an error with its own: "+
282+
"server verified:%t, client verified:%t", serverVerified, clientVerified)
283+
}
290284

291-
log.Debug("Registering namespace ", data.Prefix)
285+
// Ensure the user input prefix doesn't contain any invalid characters.
286+
// Also remove the trailing slash if it exists.
287+
reqPrefix, err := validatePrefix(data.Prefix)
288+
if err != nil {
289+
err = errors.Wrapf(err, "requested namespace %s failed validation", data.Prefix)
290+
log.Errorln(err)
291+
return false, nil, badRequestError{Message: err.Error()}
292+
}
293+
data.Prefix = reqPrefix
292294

293-
// Check if prefix exists before doing anything else
294-
exists, err := namespaceExistsByPrefix(data.Prefix)
295-
if err != nil {
296-
log.Errorf("Failed to check if namespace already exists: %v", err)
297-
return false, nil, errors.Wrap(err, "Server encountered an error checking if namespace already exists")
298-
}
299-
if exists {
300-
returnMsg := map[string]interface{}{
301-
"message": fmt.Sprintf("The prefix %s is already registered -- nothing else to do!", data.Prefix),
302-
}
303-
log.Infof("Skipping registration of prefix %s because it's already registered.", data.Prefix)
304-
return false, returnMsg, nil
305-
}
295+
log.Debug("Registering namespace ", data.Prefix)
306296

307-
inTopo, topoNss, valErr, sysErr := validateKeyChaining(data.Prefix, key)
308-
if valErr != nil {
309-
log.Errorln(err)
310-
return false, nil, permissionDeniedError{Message: valErr.Error()}
311-
}
312-
if sysErr != nil {
313-
log.Errorln(err)
314-
return false, nil, sysErr
297+
// Check if prefix exists before doing anything else
298+
exists, err := namespaceExistsByPrefix(data.Prefix)
299+
if err != nil {
300+
log.Errorf("Failed to check if namespace already exists: %v", err)
301+
return false, nil, errors.Wrap(err, "Server encountered an error checking if namespace already exists")
302+
}
303+
if exists {
304+
returnMsg := map[string]interface{}{
305+
"message": fmt.Sprintf("The prefix %s is already registered -- nothing else to do!", data.Prefix),
315306
}
307+
log.Infof("Skipping registration of prefix %s because it's already registered.", data.Prefix)
308+
return false, returnMsg, nil
309+
}
310+
311+
inTopo, topoNss, valErr, sysErr := validateKeyChaining(data.Prefix, key)
312+
if valErr != nil {
313+
log.Errorln(err)
314+
return false, nil, permissionDeniedError{Message: valErr.Error()}
315+
}
316+
if sysErr != nil {
317+
log.Errorln(err)
318+
return false, nil, sysErr
319+
}
320+
321+
var ns server_structs.Namespace
322+
ns.Prefix = data.Prefix
316323

317-
var ns server_structs.Namespace
318-
ns.Prefix = data.Prefix
324+
pubkeyData, err := json.Marshal(data.Pubkey)
325+
if err != nil {
326+
return false, nil, errors.Wrapf(err, "Failed to convert public key from json to string format for the prefix %s", ns.Prefix)
327+
}
328+
ns.Pubkey = string(pubkeyData)
329+
ns.Identity = data.Identity
330+
ns.AdminMetadata.SiteName = data.SiteName
319331

320-
pubkeyData, err := json.Marshal(data.Pubkey)
332+
if data.Identity != "" {
333+
idMap := map[string]interface{}{}
334+
err := json.Unmarshal([]byte(data.Identity), &idMap)
321335
if err != nil {
322-
return false, nil, errors.Wrapf(err, "Failed to convert public key from json to string format for the prefix %s", ns.Prefix)
336+
log.Errorln("Failed to decode non-empty Identity field:", err)
337+
return false, nil, err
323338
}
324-
ns.Pubkey = string(pubkeyData)
325-
ns.Identity = data.Identity
326-
ns.AdminMetadata.SiteName = data.SiteName
327-
328-
if data.Identity != "" {
329-
idMap := map[string]interface{}{}
330-
err := json.Unmarshal([]byte(data.Identity), &idMap)
331-
if err != nil {
332-
log.Errorln("Failed to decode non-empty Identity field:", err)
333-
return false, nil, err
334-
}
335-
sub, ok := idMap["sub"]
339+
sub, ok := idMap["sub"]
340+
if ok {
341+
val, ok := sub.(string)
336342
if ok {
337-
val, ok := sub.(string)
338-
if ok {
339-
ns.AdminMetadata.UserID = val
340-
}
343+
ns.AdminMetadata.UserID = val
341344
}
342-
if inTopo {
343-
topoNssStr := GetTopoPrefixString(topoNss)
344-
ns.AdminMetadata.Description = fmt.Sprintf("[ Attention: A superspace or subspace of this prefix exists in OSDF topology: %s ] ", topoNssStr)
345-
}
346-
userName, ok := idMap["name"]
345+
}
346+
if inTopo {
347+
topoNssStr := GetTopoPrefixString(topoNss)
348+
ns.AdminMetadata.Description = fmt.Sprintf("[ Attention: A superspace or subspace of this prefix exists in OSDF topology: %s ] ", topoNssStr)
349+
}
350+
userName, ok := idMap["name"]
351+
if ok {
352+
val, ok := userName.(string)
347353
if ok {
348-
val, ok := userName.(string)
349-
if ok {
350-
ns.AdminMetadata.Description += "User name: " + val + " "
351-
}
354+
ns.AdminMetadata.Description += "User name: " + val + " "
352355
}
353-
email, ok := idMap["email"]
356+
}
357+
email, ok := idMap["email"]
358+
if ok {
359+
val, ok := email.(string)
354360
if ok {
355-
val, ok := email.(string)
356-
if ok {
357-
ns.AdminMetadata.Description += "User email: " + val + " This is a namespace registration from Pelican CLI with OIDC authentication. Certain fields may not be populated"
358-
}
359-
}
360-
} else {
361-
// This is either a registration from CLI without --with-identity flag or
362-
// an automated registration from origin or cache
363-
ns.AdminMetadata.Description = "This is a namespace registration from Pelican CLI or an automated registration. Certain fields may not be populated"
364-
365-
// If the namespace is in the topology, we require identity information to register a Pelican namespace
366-
// for verification purpose
367-
if inTopo {
368-
return false,
369-
nil,
370-
permissionDeniedError{Message: fmt.Sprintf("A superspace or subspace of this namespace %s already exists in the OSDF topology: %s. "+
371-
"To register a Pelican equivalence, you need to present your identity. "+
372-
"If you are registering through Pelican CLI, try again with the flag '--with-identity' enabled. "+
373-
"If this is an auto-registration from a Pelican origin or cache server, "+
374-
"register your namespace or server through the Pelican registry website at %s instead.",
375-
ns.Prefix,
376-
GetTopoPrefixString(topoNss),
377-
registryUrl)}
361+
ns.AdminMetadata.Description += "User email: " + val + " This is a namespace registration from Pelican CLI with OIDC authentication. Certain fields may not be populated"
378362
}
379363
}
364+
} else {
365+
// This is either a registration from CLI without --with-identity flag or
366+
// an automated registration from origin or cache
367+
ns.AdminMetadata.Description = "This is a namespace registration from Pelican CLI or an automated registration. Certain fields may not be populated"
368+
369+
// If the namespace is in the topology, we require identity information to register a Pelican namespace
370+
// for verification purpose
371+
if inTopo {
372+
return false,
373+
nil,
374+
permissionDeniedError{Message: fmt.Sprintf("A superspace or subspace of this namespace %s already exists in the OSDF topology: %s. "+
375+
"To register a Pelican equivalence, you need to present your identity. "+
376+
"If you are registering through Pelican CLI, try again with the flag '--with-identity' enabled. "+
377+
"If this is an auto-registration from a Pelican origin or cache server, "+
378+
"register your namespace or server through the Pelican registry website at %s instead.",
379+
ns.Prefix,
380+
GetTopoPrefixString(topoNss),
381+
registryUrl)}
382+
}
383+
}
380384

381-
// Overwrite status to Pending to filter malicious request
382-
ns.AdminMetadata.Status = server_structs.RegPending
385+
// Overwrite status to Pending to filter malicious request
386+
ns.AdminMetadata.Status = server_structs.RegPending
383387

384-
err = AddNamespace(&ns)
385-
if err != nil {
386-
return false, nil, errors.Wrapf(err, "Failed to add the prefix %q to the database", ns.Prefix)
387-
} else {
388-
msg := fmt.Sprintf("Prefix %s successfully registered", ns.Prefix)
389-
if inTopo {
390-
msg = fmt.Sprintf("Prefix %s successfully registered. Note that there is an existing superspace or subspace of the namespace in the OSDF topology: %s. The registry admin will review your request and approve your namespace if this is expected.", ns.Prefix, GetTopoPrefixString(topoNss))
391-
}
392-
return true, map[string]interface{}{
393-
"message": msg,
394-
}, nil
395-
}
388+
err = AddNamespace(&ns)
389+
if err != nil {
390+
return false, nil, errors.Wrapf(err, "Failed to add the prefix %q to the database", ns.Prefix)
396391
} else {
397-
return false, nil, errors.Errorf("Unable to verify the client's public key, or an encountered an error with its own: "+
398-
"server verified:%t, client verified:%t", serverVerified, clientVerified)
392+
msg := fmt.Sprintf("Prefix %s successfully registered", ns.Prefix)
393+
if inTopo {
394+
msg = fmt.Sprintf("Prefix %s successfully registered. Note that there is an existing superspace or subspace of the namespace in the OSDF topology: %s. The registry admin will review your request and approve your namespace if this is expected.", ns.Prefix, GetTopoPrefixString(topoNss))
395+
}
396+
return true, map[string]interface{}{
397+
"message": msg,
398+
}, nil
399399
}
400+
400401
}
401402

402403
// Handle the namespace registration with nonce generation and verification, regardless of

server_utils/resources/posix-origins/multi-export-trailing-slash.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Origin export configuration to test if the Director can strip trailing slashes
1+
# Origin export configuration to test if the Director can strip trailing slashes
22
# in the advertising prefixes
33

44
Origin:
@@ -10,4 +10,4 @@ Origin:
1010
Capabilities: ["PublicReads", "Writes"]
1111
- StoragePrefix: /bar
1212
FederationPrefix: /
13-
Capabilities: ["Reads"]
13+
Capabilities: ["Reads"]

0 commit comments

Comments
 (0)