Skip to content

Commit ef0f607

Browse files
Fixing max length of userAgent header (Azure#22018)
* Fixing max length of userAgent header * Addressed CR feedback * Restricting the total UserAgent length to 255 characters * Fixing unit test regression
1 parent 9c576de commit ef0f607

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

sdk/cosmos/azure-cosmos-spark_3-1_2-12/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<includes>
166166
<include>META-INF/project.properties</include>
167167
<include>META-INF/services/org.apache.spark.sql.sources.DataSourceRegister</include>
168+
<include>azure-cosmos-spark.properties</include>
168169
</includes>
169170
</resource>
170171
</resources>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name=${project.artifactId}
2+
version=${project.version}

sdk/cosmos/azure-cosmos-spark_3-1_2-12/src/main/scala/com/azure/cosmos/spark/CosmosConstants.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import com.azure.cosmos.implementation.HttpConstants
88

99
// cosmos db related constants
1010
private object CosmosConstants {
11-
private[this] val currentVersion =
12-
CoreUtils.getProperties(HttpConstants.Versions.AZURE_COSMOS_PROPERTIES_FILE_NAME).get("version")
13-
val userAgentSuffix = s" SparkConnector/$currentVersion"
11+
private[this] val propertiesFileName = "azure-cosmos-spark.properties"
12+
val currentVersion: String =
13+
CoreUtils.getProperties(propertiesFileName).get("version")
14+
val currentName: String =
15+
CoreUtils.getProperties(propertiesFileName).get("name")
16+
val userAgentSuffix = s"SparkConnector/$currentName/$currentVersion"
1417

1518
object Names {
1619
val ItemsDataSourceShortName = "cosmos.oltp"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.cosmos.spark
4+
5+
class CosmosConstantsSpec extends UnitSpec {
6+
"CurrentVersion" should "not be null" in {
7+
CosmosConstants.currentVersion == null shouldBe false
8+
CosmosConstants.currentVersion.startsWith("4.") shouldBe true
9+
}
10+
11+
"CurrentName" should "not be null" in {
12+
CosmosConstants.currentName == null shouldBe false
13+
CosmosConstants.currentName.startsWith("azure-cosmos-spark") shouldBe true
14+
}
15+
16+
"UserAgentSuffix" should "combine name and version" in {
17+
CosmosConstants.userAgentSuffix shouldBe
18+
s"SparkConnector/${CosmosConstants.currentName}/${CosmosConstants.currentVersion}"
19+
}
20+
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/UserAgentContainer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
public class UserAgentContainer {
1010

11-
private static final int MAX_SUFFIX_LENGTH = 64;
11+
private static final int MAX_USER_AGENT_LENGTH = 255;
12+
private final int maxSuffixLength;
1213
private final String baseUserAgent;
1314
private String suffix;
1415
private String userAgent;
@@ -18,6 +19,7 @@ private UserAgentContainer(String sdkName, String sdkVersion) {
1819
this.baseUserAgent = Utils.getUserAgent(sdkName, sdkVersion);
1920
this.suffix = "";
2021
this.userAgent = baseUserAgent;
22+
this.maxSuffixLength = MAX_USER_AGENT_LENGTH - 1 - baseUserAgent.length();
2123
}
2224

2325
public UserAgentContainer() {
@@ -29,8 +31,8 @@ public String getSuffix() {
2931
}
3032

3133
public void setSuffix(String suffix) {
32-
if (suffix.length() > MAX_SUFFIX_LENGTH) {
33-
suffix = suffix.substring(0, MAX_SUFFIX_LENGTH);
34+
if (suffix.length() > maxSuffixLength) {
35+
suffix = suffix.substring(0, maxSuffixLength);
3436
}
3537

3638
this.suffix = suffix;

sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/implementation/UserAgentContainerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public void userAgentContainerSetSuffix() {
3131
assertThat(userAgentContainer.getUserAgent()).isEqualTo(expectedString);
3232

3333
//With suffix greater than 64 character
34-
userProvidedSuffix = "greater than 64 characters ###########################################";
34+
userProvidedSuffix = "greater than 255 characters in total ################################################" +
35+
"##########################################################################################################" +
36+
"##########################################################################################################";
3537
userAgentContainer = new UserAgentContainer();
3638
userAgentContainer.setSuffix(userProvidedSuffix);
37-
expectedString = expectedStringFixedPart + SPACE + userProvidedSuffix.substring(0, 64);
39+
expectedString = (expectedStringFixedPart + SPACE + userProvidedSuffix).substring(0, 255);
3840
assertThat(userAgentContainer.getUserAgent()).isEqualTo(expectedString);
3941
}
4042

0 commit comments

Comments
 (0)