-
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 6ec90c2
Showing
22 changed files
with
478 additions
and
103 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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
/* | ||
* 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 [ ] | ||
|
||
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)) { | ||
int attrStart = THING_ATTRS_PREFIX.length(); | ||
int attrEnd = policyVariable.length() - THING_ATTRS_SUFFIX.length(); | ||
if (attrStart <= attrEnd) { | ||
String attr = policyVariable.substring(attrStart, attrEnd); | ||
if (StringUtils.isAlphanumeric(attr)) { | ||
return Optional.of(PolicyVariable.builder() | ||
.originalText(policyVariable) | ||
.attribute(Attribute.THING_ATTRIBUTES) | ||
.selector(attr) | ||
.build()); | ||
} | ||
} | ||
} | ||
|
||
// unsupported variable | ||
return Optional.empty(); | ||
} | ||
} |
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
Oops, something went wrong.