Skip to content

Commit be0e584

Browse files
committed
Reject __settings__ as a reserved profile target
Keep default_profile resolution aligned with the other SDKs by preserving the resolved profile name and surfacing bad settings targets instead of silently falling back.
1 parent 39ecf38 commit be0e584

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

databricks-sdk-java/src/main/java/com/databricks/sdk/core/ConfigLoader.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@InternalApi
2020
public class ConfigLoader {
2121
private static final Logger LOG = LoggerFactory.getLogger(ConfigLoader.class);
22+
private static final String SETTINGS_SECTION = "__settings__";
2223

2324
private static final List<ConfigAttributeAccessor> accessors = attributeAccessors();
2425

@@ -94,30 +95,36 @@ static void loadFromConfig(DatabricksConfig cfg) throws IllegalAccessException {
9495

9596
String profile = cfg.getProfile();
9697
boolean hasExplicitProfile = !isNullOrEmpty(profile);
98+
boolean hasDefaultProfileSetting = false;
9799
if (!hasExplicitProfile) {
98-
SubnodeConfiguration settings = ini.getSection("__settings__");
100+
SubnodeConfiguration settings = ini.getSection(SETTINGS_SECTION);
99101
if (settings != null && !settings.isEmpty()) {
100102
String defaultProfile = settings.getString("default_profile");
101-
if (!isNullOrEmpty(defaultProfile) && !"__settings__".equals(defaultProfile)) {
103+
if (defaultProfile != null) {
104+
defaultProfile = defaultProfile.trim();
105+
}
106+
if (!isNullOrEmpty(defaultProfile)) {
102107
profile = defaultProfile;
103-
hasExplicitProfile = true;
104-
cfg.setProfile(profile);
108+
hasDefaultProfileSetting = true;
105109
}
106110
}
107111
}
108-
if (!hasExplicitProfile) {
112+
if (!hasExplicitProfile && !hasDefaultProfileSetting) {
109113
profile = "DEFAULT";
110114
}
111-
SubnodeConfiguration section = ini.getSection(profile);
115+
SubnodeConfiguration section = SETTINGS_SECTION.equals(profile) ? null : ini.getSection(profile);
112116
boolean sectionNotPresent = section == null || section.isEmpty();
113-
if (sectionNotPresent && !hasExplicitProfile) {
117+
if (sectionNotPresent && !hasExplicitProfile && !hasDefaultProfileSetting) {
114118
LOG.info("{} has no {} profile configured", configFile, profile);
115119
return;
116120
}
117121
if (sectionNotPresent) {
118122
String msg = String.format("resolve: %s has no %s profile configured", configFile, profile);
119123
throw new DatabricksException(msg);
120124
}
125+
if (hasDefaultProfileSetting) {
126+
cfg.setProfile(profile);
127+
}
121128

122129
for (ConfigAttributeAccessor accessor : accessors) {
123130
String value = section.getString(accessor.getName());

databricks-sdk-java/src/test/java/com/databricks/sdk/DefaultProfileTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,24 @@ public void testLegacyFallbackWhenDefaultProfileEmpty() {
7373
assertEquals("https://default.cloud.databricks.com", config.getHost());
7474
}
7575

76-
/** Test 5: default_profile = __settings__ is rejected and falls back to DEFAULT */
76+
/** Test 5: default_profile = __settings__ is rejected */
7777
@Test
7878
public void testSettingsSelfReferenceIsRejected() {
7979
StaticEnv env =
8080
new StaticEnv()
8181
.with("HOME", TestOSUtils.resource("/testdata/default_profile_settings_self_ref"));
8282
DatabricksConfig config = createConfigWithMockClient();
83-
resolveConfig(config, env);
84-
config.authenticate();
8583

86-
// __settings__ as a profile target should be ignored, falling back to [DEFAULT]
87-
assertEquals("https://default.cloud.databricks.com", config.getHost());
88-
assertEquals("pat", config.getAuthType());
84+
DatabricksException ex =
85+
assertThrows(
86+
DatabricksException.class,
87+
() -> {
88+
resolveConfig(config, env);
89+
config.authenticate();
90+
});
91+
assertTrue(
92+
ex.getMessage().contains("has no __settings__ profile configured"),
93+
"Error should reject __settings__ as a profile target: " + ex.getMessage());
8994
}
9095

9196
/** Test 6: Explicit --profile overrides default_profile */
@@ -103,6 +108,26 @@ public void testExplicitProfileOverridesDefaultProfile() {
103108
assertEquals("https://other.cloud.databricks.com", config.getHost());
104109
}
105110

111+
@Test
112+
public void testExplicitSettingsSectionProfileIsRejected() {
113+
StaticEnv env =
114+
new StaticEnv()
115+
.with("DATABRICKS_CONFIG_PROFILE", "__settings__")
116+
.with("HOME", TestOSUtils.resource("/testdata/default_profile"));
117+
DatabricksConfig config = createConfigWithMockClient();
118+
119+
DatabricksException ex =
120+
assertThrows(
121+
DatabricksException.class,
122+
() -> {
123+
resolveConfig(config, env);
124+
config.authenticate();
125+
});
126+
assertTrue(
127+
ex.getMessage().contains("has no __settings__ profile configured"),
128+
"Error should reject __settings__ as a profile target: " + ex.getMessage());
129+
}
130+
106131
/** Test 7: default_profile pointing to nonexistent section */
107132
@Test
108133
public void testDefaultProfileNonexistentSection() {

0 commit comments

Comments
 (0)