-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add thing attribute variable support
- Loading branch information
1 parent
34b9a58
commit fdfd350
Showing
22 changed files
with
512 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/aws/greengrass/clientdevices/auth/configuration/PolicyVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.clientdevices.auth.configuration; | ||
|
||
import com.aws.greengrass.clientdevices.auth.session.attribute.Attribute; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Builder | ||
@Value | ||
public class PolicyVariable { | ||
|
||
private static final String THING_NAME_PATTERN = "${iot:Connection.Thing.ThingName}"; | ||
private static final String THING_NAMESPACE = "Thing"; | ||
|
||
private static final String THING_ATTRS_PREFIX = "${iot:Connection.Thing.Attributes["; | ||
private static final String THING_ATTRS_SUFFIX = "]}"; | ||
|
||
String originalText; | ||
boolean isThingAttribute; | ||
Attribute attribute; | ||
String selector; // the part within [ ] | ||
|
||
/** | ||
* Parse a policy variable from string. | ||
* | ||
* @param policyVariable variable | ||
* @return parsed policy variable | ||
*/ | ||
public static Optional<PolicyVariable> parse(@NonNull String policyVariable) { | ||
// thing name | ||
if (Objects.equals(policyVariable, THING_NAME_PATTERN)) { | ||
return Optional.of(PolicyVariable.builder() | ||
.originalText(policyVariable) | ||
.attribute(Attribute.THING_NAME) | ||
.build()); | ||
} | ||
|
||
// thing attributes | ||
if (policyVariable.startsWith(THING_ATTRS_PREFIX) && policyVariable.endsWith(THING_ATTRS_SUFFIX)) { | ||
return parseAttributePolicyVariable(policyVariable); | ||
} | ||
|
||
// unsupported variable | ||
return Optional.empty(); | ||
} | ||
|
||
private static Optional<PolicyVariable> parseAttributePolicyVariable(@NonNull String policyVariable) { | ||
int attrStart = THING_ATTRS_PREFIX.length(); | ||
int attrEnd = policyVariable.length() - THING_ATTRS_SUFFIX.length(); | ||
if (attrStart > attrEnd) { | ||
return Optional.empty(); | ||
} | ||
|
||
String attr = policyVariable.substring(attrStart, attrEnd); | ||
if (!StringUtils.isAlphanumeric(attr)) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(PolicyVariable.builder() | ||
.originalText(policyVariable) | ||
.attribute(Attribute.THING_ATTRIBUTES) | ||
.selector(attr) | ||
.build()); | ||
} | ||
} |
Oops, something went wrong.