-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't return secrets in the API & Only update credentials when requested #1022
Changes from 1 commit
dcb5d38
4984a34
90b9993
5096c36
d763ef8
5e0235a
8c10acf
7e5e31c
f8d63fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Airbyte | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package io.airbyte.commons.json; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class JsonSecretsProcessor { | ||
|
||
public static String AIRBYTE_SECRET_FIELD = "airbyte_secret"; | ||
private static String PROPERTIES_FIELD = "properties"; | ||
|
||
/** | ||
* Returns a copy of the input object wherein any fields annotated with "airbyte_secret" in the | ||
* input schema are removed. | ||
* <p> | ||
* TODO this method only removes secrets at the top level of the configuration object. It does not support the keywords anyOf, allOf, oneOf, not, and | ||
* dependencies. This will be fixed in the future. | ||
* | ||
* @param schema Schema containing secret annotations | ||
* @param obj Object containing potentially secret fields | ||
* @return | ||
*/ | ||
public JsonNode removeSecrets(JsonNode obj, JsonNode schema) { | ||
assertValidSchema(schema); | ||
Preconditions.checkArgument(schema.isObject()); | ||
|
||
ObjectNode properties = (ObjectNode) schema.get(PROPERTIES_FIELD); | ||
JsonNode copy = obj.deepCopy(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deepCopy can return a different type e.g: |
||
for (String key : Jsons.keys(properties)) { | ||
if (isSecret(properties.get(key))) { | ||
((ObjectNode) copy).remove(key); | ||
} | ||
} | ||
|
||
return copy; | ||
} | ||
|
||
/** | ||
* Returns a copy of the destination object in which any secret fields (as denoted by the input | ||
* schema) found in the source object are added. | ||
* <p> | ||
* TODO this method only absorbs secrets at the top level of the configuration object. It does not support the keywords anyOf, allOf, oneOf, not, and | ||
* dependencies. This will be fixed in the future. | ||
* | ||
* @param src The object potentially containing secrets | ||
* @param dst The object to absorb secrets into | ||
* @param schema | ||
* @return | ||
*/ | ||
public JsonNode copySecrets(JsonNode src, JsonNode dst, JsonNode schema) { | ||
assertValidSchema(schema); | ||
Preconditions.checkArgument(dst.isObject()); | ||
Preconditions.checkArgument(src.isObject()); | ||
|
||
ObjectNode dstCopy = dst.deepCopy(); | ||
|
||
ObjectNode properties = (ObjectNode) schema.get(PROPERTIES_FIELD); | ||
for (String key : Jsons.keys(properties)) { | ||
if (isSecret(properties.get(key)) && src.has(key)) { | ||
dstCopy.set(key, src.get(key)); | ||
} | ||
} | ||
|
||
return dstCopy; | ||
} | ||
|
||
private static boolean isSecret(JsonNode obj) { | ||
return obj.isObject() && obj.has(AIRBYTE_SECRET_FIELD) && obj.get(AIRBYTE_SECRET_FIELD).asBoolean(); | ||
} | ||
|
||
private static void assertValidSchema(JsonNode node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we want to throw is a schema doesn't match our expectations? don't we just want to do nothing? e.g. if there is no top level properties and it is just a oneOf we don't want to fail do we? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough. will fix |
||
Preconditions.checkArgument(node.isObject()); | ||
Preconditions.checkArgument(node.has(PROPERTIES_FIELD), "Schema object must have a properties field"); | ||
Preconditions.checkArgument(node.get(PROPERTIES_FIELD).isObject(), "Properties field must be a JSON object"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.airbyte.commons.json; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airbyte.commons.resources.MoreResources; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class JsonSecretsProcessorTest { | ||
|
||
JsonSecretsProcessor processor = new JsonSecretsProcessor(); | ||
|
||
@Test | ||
public void testRemoveSecrets() throws IOException { | ||
JsonNode obj = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.put("secret1", "donttellanyone") | ||
.put("secret2", 12345).build()); | ||
JsonNode schema = Jsons.deserialize(MoreResources.readResource("secrets_json_schema.json")); | ||
|
||
JsonNode sanitized = processor.removeSecrets(obj, schema); | ||
|
||
JsonNode expected = Jsons.jsonNode(ImmutableMap.of("field1", "value1", "field2", 2)); | ||
assertEquals(expected, sanitized); | ||
} | ||
|
||
@Test | ||
public void testRemoveSecretsNotInObj() throws IOException { | ||
JsonNode schema = Jsons.deserialize(MoreResources.readResource("secrets_json_schema.json")); | ||
JsonNode obj = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2).build()); | ||
|
||
JsonNode actual = processor.removeSecrets(obj, schema); | ||
|
||
// Didn't have secrets, no fields should have been impacted. | ||
assertEquals(obj, actual); | ||
} | ||
|
||
@Test | ||
public void testCopySecrets() throws IOException { | ||
JsonNode src = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.put("additional_field", "dont_copy_me") | ||
.put("secret1", "donttellanyone") | ||
.put("secret2", 12345) | ||
.build()); | ||
|
||
JsonNode dst = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.build()); | ||
|
||
JsonNode schema = Jsons.deserialize(MoreResources.readResource("secrets_json_schema.json")); | ||
|
||
JsonNode actual = processor.copySecrets(src, dst, schema); | ||
|
||
JsonNode expected = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.put("secret1", "donttellanyone") | ||
.put("secret2", 12345) | ||
.build()); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void testCopySecretsNotInSrc() throws IOException { | ||
JsonNode schema = Jsons.deserialize(MoreResources.readResource("secrets_json_schema.json")); | ||
JsonNode src = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.put("additional_field", "dont_copy_me") | ||
.build()); | ||
|
||
JsonNode dst = Jsons.jsonNode(ImmutableMap.builder() | ||
.put("field1", "value1") | ||
.put("field2", 2) | ||
.build()); | ||
|
||
JsonNode expected = dst.deepCopy(); | ||
JsonNode actual = processor.copySecrets(src, dst, schema); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"properties": { | ||
"secret1": { | ||
"type": "string", | ||
"airbyte_secret": true | ||
}, | ||
"secret2": { | ||
"type": "number", | ||
"airbyte_secret": "true" | ||
}, | ||
"field1": { | ||
"type": "string" | ||
}, | ||
"field2": { | ||
"type": "number" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,5 @@ | |
SOFTWARE. | ||
""" | ||
|
||
|
||
def test_example_method(): | ||
assert True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this strategy is different from the originally proposed one in the issue #986. Why the change?
How does this implementation handle the case where a configuration has 2 secrets and only one is updated?