Skip to content

Commit ecb47e9

Browse files
authored
chore: Improve service binding logic (#636)
* improve service binding logic * improve tests * release notes
1 parent d2db8a7 commit ecb47e9

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

core/src/main/java/com/sap/ai/sdk/core/AiCoreServiceKeyAccessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ static ServiceBinding createServiceBinding(@Nonnull final String serviceKey)
8181
new AiCoreCredentialsInvalidException("Missing clientid in service key"));
8282
}
8383

84+
if (credentials.get("clientsecret") != null && credentials.get("credential-type") == null) {
85+
// add missing "credential-type: binding-secret"
86+
credentials.put("credential-type", "binding-secret");
87+
}
88+
8489
return new DefaultServiceBindingBuilder()
8590
.withServiceIdentifier(ServiceIdentifier.AI_CORE)
8691
.withCredentials(credentials)

core/src/test/java/com/sap/ai/sdk/core/AiCoreServiceKeyAccessorTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,32 @@ void testMalformedDotenv() {
5555
assertThatThrownBy(accessor::getServiceBindings)
5656
.isInstanceOf(ServiceBindingAccessException.class);
5757
}
58+
59+
@Test
60+
void testServiceBindingCreation() {
61+
var serviceKeyWithoutCredentialType =
62+
"""
63+
{
64+
"clientid": "",
65+
"clientsecret": ""
66+
}
67+
""";
68+
var binding1 = AiCoreServiceKeyAccessor.createServiceBinding(serviceKeyWithoutCredentialType);
69+
assertThat(binding1.getCredentials().containsKey("credential-type"))
70+
.describedAs("The missing 'credential-type' should have automatically been added")
71+
.isTrue();
72+
var serviceKeyWithCredentialType =
73+
"""
74+
{
75+
"clientid": "",
76+
"clientsecret": "",
77+
"credential-type": "foo"
78+
}
79+
}
80+
""";
81+
var binding2 = AiCoreServiceKeyAccessor.createServiceBinding(serviceKeyWithCredentialType);
82+
assertThat(binding2.getCredentials().get("credential-type"))
83+
.describedAs("The 'credential-type' field should not get overwritten")
84+
.isEqualTo("foo");
85+
}
5886
}

docs/release_notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
### 📈 Improvements
1818

19-
-
19+
- [Core] If the AI Core credentials used are missing an explicit `credential-type` but `clientid` and `clientsecret` are present then `"credential-type": "binding-secret"` is inferred automatically.
20+
2021

2122
### 🐛 Fixed Issues
2223

0 commit comments

Comments
 (0)