Skip to content

Commit

Permalink
Deduplicate identifier parsing
Browse files Browse the repository at this point in the history
Resolves Azure#24262
  • Loading branch information
heaths committed Dec 1, 2021
1 parent 5a0f002 commit f90123f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,14 @@ internal void ReadProperty(JsonProperty prop)
}
}

private void ParseId(Uri idToParse)
private void ParseId(Uri id)
{
// We expect an identifier with either 3 or 4 segments: host + collection + name [+ version]
if (idToParse.Segments.Length != 3 && idToParse.Segments.Length != 4)
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. Bad number of segments: {1}", idToParse, idToParse.Segments.Length));
KeyVaultIdentifier identifier = KeyVaultIdentifier.ParseWithCollection(id, "certificates");

if (!string.Equals(idToParse.Segments[1], "certificates" + "/", StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. segment [1] should be 'certificates/', found '{1}'", idToParse, idToParse.Segments[1]));

VaultUri = new Uri($"{idToParse.Scheme}://{idToParse.Authority}");
Name = idToParse.Segments[2].Trim('/');
Version = (idToParse.Segments.Length == 4) ? idToParse.Segments[3].TrimEnd('/') : null;
Id = id;
VaultUri = identifier.VaultUri;
Name = identifier.Name;
Version = identifier.Version;
}
}
}
19 changes: 7 additions & 12 deletions sdk/keyvault/Azure.Security.KeyVault.Keys/src/KeyProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,15 @@ public KeyProperties(Uri id)
/// <summary>
/// Parses the key identifier into the <see cref="VaultUri"/>, <see cref="Name"/>, and <see cref="Version"/> of the key.
/// </summary>
/// <param name="idToParse">The key vault object identifier.</param>
internal void ParseId(Uri idToParse)
/// <param name="id">The key vault object identifier.</param>
internal void ParseId(Uri id)
{
// We expect an identifier with either 3 or 4 segments: host + collection + name [+ version]
if (idToParse.Segments.Length != 3 && idToParse.Segments.Length != 4)
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. Bad number of segments: {1}", idToParse, idToParse.Segments.Length));
KeyVaultIdentifier identifier = KeyVaultIdentifier.ParseWithCollection(id, "keys");

if (!string.Equals(idToParse.Segments[1], "keys" + "/", StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. segment [1] should be 'keys/', found '{1}'", idToParse, idToParse.Segments[1]));

Id = idToParse;
VaultUri = new Uri($"{idToParse.Scheme}://{idToParse.Authority}");
Name = idToParse.Segments[2].Trim('/');
Version = (idToParse.Segments.Length == 4) ? idToParse.Segments[3].TrimEnd('/') : null;
Id = id;
VaultUri = identifier.VaultUri;
Name = identifier.Name;
Version = identifier.Version;
}

internal void ReadProperty(JsonProperty prop)
Expand Down
37 changes: 0 additions & 37 deletions sdk/keyvault/Azure.Security.KeyVault.Secrets/src/ObjectId.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class SecretProperties : IJsonDeserializable, IJsonSerializable
private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropertyName);
private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode(TagsPropertyName);

private ObjectId _identifier;
private SecretAttributes _attributes;
private Dictionary<string, string> _tags;
private string _keyId;
Expand All @@ -44,7 +43,7 @@ public SecretProperties(string name)
{
Argument.AssertNotNullOrEmpty(name, nameof(name));

_identifier.Name = name;
Name = name;
}

/// <summary>
Expand All @@ -56,28 +55,28 @@ public SecretProperties(Uri id)
{
Argument.AssertNotNull(id, nameof(id));

_identifier.ParseId("secrets", id);
ParseId(id);
}

/// <summary>
/// Gets the secret identifier.
/// </summary>
public Uri Id { get => _identifier.Id; internal set => _identifier.Id = value; }
public Uri Id { get; internal set; }

/// <summary>
/// Gets the Key Vault base <see cref="Uri"/>.
/// </summary>
public Uri VaultUri { get => _identifier.VaultUri; internal set => _identifier.VaultUri = value; }
public Uri VaultUri { get; internal set; }

/// <summary>
/// Gets the name of the secret.
/// </summary>
public string Name { get => _identifier.Name; internal set => _identifier.Name = value; }
public string Name { get; internal set; }

/// <summary>
/// Gets the version of the secret.
/// </summary>
public string Version { get => _identifier.Version; internal set => _identifier.Version = value; }
public string Version { get; internal set; }

/// <summary>
/// Gets or sets the content type of the secret value such as "text/plain" for a password.
Expand Down Expand Up @@ -142,6 +141,20 @@ public Uri KeyId
/// </summary>
public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags);

/// <summary>
/// Parses the key identifier into the <see cref="VaultUri"/>, <see cref="Name"/>, and <see cref="Version"/> of the key.
/// </summary>
/// <param name="id">The key vault object identifier.</param>
internal void ParseId(Uri id)
{
KeyVaultIdentifier identifier = KeyVaultIdentifier.ParseWithCollection(id, "secrets");

Id = id;
VaultUri = identifier.VaultUri;
Name = identifier.Name;
Version = identifier.Version;
}

internal void ReadProperties(JsonElement json)
{
foreach (JsonProperty prop in json.EnumerateObject())
Expand All @@ -155,7 +168,9 @@ internal void ReadProperty(JsonProperty prop)
switch (prop.Name)
{
case IdPropertyName:
_identifier.ParseId("secrets", prop.Value.GetString());
string id = prop.Value.GetString();
Id = new Uri(id);
ParseId(Id);
break;

case ContentTypePropertyName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public static KeyVaultIdentifier Parse(Uri id)
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. Bad number of segments: {1}", id, id.Segments.Length));
}

public static KeyVaultIdentifier ParseWithCollection(Uri id, string collection)
{
KeyVaultIdentifier identifier = Parse(id);
if (!string.Equals(identifier.Collection, collection, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. segment [1] should be '{1}/', found '{2}'", id, collection, identifier.Collection));
}

return identifier;
}

public static bool TryParse(Uri id, out KeyVaultIdentifier identifier)
{
if (id is null)
Expand Down

0 comments on commit f90123f

Please sign in to comment.