Skip to content

Commit

Permalink
common: trim tailing zero when calculating sign for double and float (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat authored Sep 27, 2022
1 parent bdc68ff commit 5268c48
Show file tree
Hide file tree
Showing 32 changed files with 173 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ucloud_ucloud-sdk-java
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dgpg.skip -Dsonar.projectKey=ucloud_ucloud-sdk-java
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.9-release
1.1.0-release
4 changes: 2 additions & 2 deletions examples/generic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>
</dependencies>
</project>
4 changes: 2 additions & 2 deletions examples/uhost/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-uhost</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java</artifactId>
<packaging>pom</packaging>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
<modules>
<module>ucloud-sdk-java-common</module>
<module>ucloud-sdk-java-cube</module>
Expand Down
4 changes: 2 additions & 2 deletions ucloud-sdk-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Map;

Expand Down Expand Up @@ -47,10 +49,21 @@ public String verifyAc(Request request) throws UCloudException {
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);

DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(Integer.MAX_VALUE);
String s = "";
for (String key : keys) {
s = s.concat(key);
s = s.concat(params.get(key).toString());
Object value = params.get(key);
String valueStr;
if (value instanceof Double || value instanceof Float) {
// If the type is double or float, we need to trim the tailing zeros
// to be consistent with the json decoder.
valueStr = df.format(value);
} else {
valueStr = value.toString();
}
s = s.concat(valueStr);
}
s = s.concat(this.privateKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,82 @@ public void verifyAc() throws UCloudException {

assertEquals("cba5cf5ec4d4233d206b1b54951e3787350a642f", cred.verifyAc(request));
}

static class DoubleRequest extends Request {
DoubleRequest() { super(); }

@UCloudParam("Proportion")
private Double proportion;

@UCloudParam("Region")
private String region;

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public void setProportion(Double proportion) {
this.proportion = proportion;
}

public Double getProportion() {
return proportion;
}
}

@Test
public void verifyAcDoubleTailingZero() throws UCloudException {
Credential cred = new Credential(pubKey, priKey);
Assert.assertEquals(pubKey, cred.getPublicKey());
Assert.assertEquals(priKey, cred.getPrivateKey());

cred.setPublicKey(pubKey);
cred.setPrivateKey(priKey);
Assert.assertEquals(pubKey, cred.getPublicKey());
Assert.assertEquals(priKey, cred.getPrivateKey());

DoubleRequest request = new DoubleRequest();
request.setAction("DescribeUHostInstance");
request.setRegion("cn-bj2");
request.setProportion(35.0);

String ac0 = cred.verifyAc(request);

// You can calculate sign by:
// ucloud sign --private-key 46f09bb9fab4f12dfc160dae12273d5332b5debe \
// --param Action=DescribeUHostInstance --param Region=cn-bj2 \
// --param Proportion=35 \
// --param [email protected]
assertEquals("da620b97de476947302291fc93a2d8b0710be328", ac0);
}

@Test
public void verifyAcDoubleNoneTailingZero() throws UCloudException {
Credential cred = new Credential(pubKey, priKey);
Assert.assertEquals(pubKey, cred.getPublicKey());
Assert.assertEquals(priKey, cred.getPrivateKey());

cred.setPublicKey(pubKey);
cred.setPrivateKey(priKey);
Assert.assertEquals(pubKey, cred.getPublicKey());
Assert.assertEquals(priKey, cred.getPrivateKey());

DoubleRequest request = new DoubleRequest();
request.setAction("DescribeUHostInstance");
request.setRegion("cn-bj2");
request.setProportion(65.722);

String ac0 = cred.verifyAc(request);

// You can calculate sign by:
// ucloud sign --private-key 46f09bb9fab4f12dfc160dae12273d5332b5debe \
// --param Action=DescribeUHostInstance --param Region=cn-bj2 \
// --param Proportion=65.722 \
// --param [email protected]
assertEquals("5e39dd48144d6a075328e56a6c5e9df6c39ae939", ac0);
}
}
6 changes: 3 additions & 3 deletions ucloud-sdk-java-cube/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-cube</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-ipsecvpn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-ipsecvpn</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-pathx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-pathx</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-uaccount/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-uaccount</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-ubill/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-ubill</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-ucdn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-ucdn</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-udb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-udb</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-uddb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-uddb</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions ucloud-sdk-java-udisk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<parent>
<artifactId>ucloud-sdk-java</artifactId>
<groupId>cn.ucloud</groupId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>ucloud-sdk-java-udisk</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>

<dependencies>
<dependency>
<groupId>cn.ucloud</groupId>
<artifactId>ucloud-sdk-java-common</artifactId>
<version>1.0.9-release</version>
<version>1.1.0-release</version>
</dependency>

<dependency>
Expand Down
Loading

0 comments on commit 5268c48

Please sign in to comment.