Skip to content

Commit

Permalink
SNOW-830955 Use default SDK version in User-Agent
Browse files Browse the repository at this point in the history
While constructing HTTP User-Agent header, take the SDK version from code instead from a file on classpath. It is less prone to accidental classpath overrides. The consequence of this is that we must now keep the DEFAULT_VERSION in sync with the project version defined in Maven, which we are doing already anyway (a new test has been added to verify this).
  • Loading branch information
sfc-gh-lsembera committed Jun 2, 2023
1 parent 9d1ab3d commit e126bc8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/TestSimpleIngestLocal.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ public class RequestBuilder {
// and object mapper for all marshalling and unmarshalling
private static final ObjectMapper objectMapper = new ObjectMapper();

private static final String RESOURCES_FILE = "project.properties";

private static final Properties PROPERTIES = loadProperties();

private static final String USER_AGENT = getDefaultUserAgent();

// Don't change!
Expand Down Expand Up @@ -298,25 +294,6 @@ public RequestBuilder(
this.userAgentSuffix);
}

private static Properties loadProperties() {
Properties properties = new Properties();
properties.put("version", DEFAULT_VERSION);

try (InputStream is =
SimpleIngestManager.class.getClassLoader().getResourceAsStream(RESOURCES_FILE)) {
if (is == null) {
throw new FileNotFoundException(RESOURCES_FILE);
}
properties.load(is);
} catch (Exception e) {
LOGGER.warn("Could not read version info, use default version " + DEFAULT_VERSION, e);
return properties;
}

LOGGER.info("Loaded project version " + properties.getProperty("version"));
return properties;
}

/**
* Creates a string for user agent which should always be present in all requests to Snowflake
* (Snowpipe APIs)
Expand All @@ -328,8 +305,7 @@ private static Properties loadProperties() {
* @return the default agent string
*/
private static String getDefaultUserAgent() {
final String clientVersion = PROPERTIES.getProperty("version");
StringBuilder defaultUserAgent = new StringBuilder(CLIENT_NAME + "/" + clientVersion);
StringBuilder defaultUserAgent = new StringBuilder(CLIENT_NAME + "/" + DEFAULT_VERSION);

final String osInformation =
String.format(
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/net/snowflake/ingest/connection/UserAgentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.snowflake.ingest.connection;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.snowflake.ingest.SimpleIngestManager;
import org.junit.Assert;
import org.junit.Test;

public class UserAgentTest {
@Test
public void testDefaultSdkVersionMatchesProjectVersion() throws IOException {
Properties properties = new Properties();
try (InputStream is =
SimpleIngestManager.class.getClassLoader().getResourceAsStream("project.properties")) {
properties.load(is);
Assert.assertEquals(RequestBuilder.DEFAULT_VERSION, properties.getProperty("version"));
}
}
}

0 comments on commit e126bc8

Please sign in to comment.