From fde680928770f0756e85d64ff48476be56a6e626 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Fri, 25 Sep 2026 16:42:39 +0300 Subject: [PATCH] Store the granted license key in tb_cluster The enrollment now collects its license key from the portal when the grant is done, and keeps it in tb_cluster.license_secret. A claim that has nothing to hand over yet leaves the flow for the next poll; a refused claim ends the flow registered without a key; a transport failure is left to the existing backoff. --- .../upgrade/lts/4.3.1.6/schema_update.sql | 8 +++ ...nityGrantLicenseClaimRefusedException.java | 16 ++++++ .../CommunityGrantLicenseClaimRequest.java | 4 ++ .../CommunityGrantLicenseClaimResponse.java | 8 +++ .../community_grant/CommunityGrantPoller.java | 44 ++++++++++++--- .../CommunityGrantPortalClient.java | 25 +++++++++ .../CommunityGrantPollerTest.java | 56 +++++++++++++++++++ .../CommunityGrantPortalClientTest.java | 48 ++++++++++++++++ .../dao/subscription/TbClusterStore.java | 13 ++++- .../main/resources/sql/schema-entities.sql | 1 + .../dao/subscription/TbClusterStoreTest.java | 27 +++++++++ 11 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRefusedException.java create mode 100644 application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRequest.java create mode 100644 application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimResponse.java diff --git a/application/src/main/data/upgrade/lts/4.3.1.6/schema_update.sql b/application/src/main/data/upgrade/lts/4.3.1.6/schema_update.sql index e984d996f5..740462d6c9 100644 --- a/application/src/main/data/upgrade/lts/4.3.1.6/schema_update.sql +++ b/application/src/main/data/upgrade/lts/4.3.1.6/schema_update.sql @@ -11,6 +11,7 @@ CREATE TABLE IF NOT EXISTS tb_cluster ( cluster_id uuid NOT NULL, license_claim_token varchar, + license_secret varchar, CONSTRAINT tb_cluster_pkey PRIMARY KEY (cluster_id) ); @@ -19,3 +20,10 @@ CREATE TABLE IF NOT EXISTS tb_cluster ( CREATE UNIQUE INDEX IF NOT EXISTS tb_cluster_single_row ON tb_cluster ((true)); -- TB_CLUSTER END + +-- TB_CLUSTER LICENSE SECRET START + +-- The CREATE above is a no-op on a database that already carries tb_cluster. +ALTER TABLE tb_cluster ADD COLUMN IF NOT EXISTS license_secret varchar; + +-- TB_CLUSTER LICENSE SECRET END diff --git a/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRefusedException.java b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRefusedException.java new file mode 100644 index 0000000000..71fc304165 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRefusedException.java @@ -0,0 +1,16 @@ +package org.thingsboard.server.service.community_grant; + +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.RestClientException; + +/** + * Thrown by {@link CommunityGrantPortalClient#claimLicense} when the portal will not hand the license key + * over. Terminal, unlike a transport failure: retrying the claim cannot change the answer. + */ +public class CommunityGrantLicenseClaimRefusedException extends RestClientException { + + public CommunityGrantLicenseClaimRefusedException(HttpStatusCodeException cause) { + super(cause.getMessage(), cause); + } + +} diff --git a/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRequest.java b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRequest.java new file mode 100644 index 0000000000..364c29f3bb --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimRequest.java @@ -0,0 +1,4 @@ +package org.thingsboard.server.service.community_grant; + +public record CommunityGrantLicenseClaimRequest(String claimToken) { +} diff --git a/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimResponse.java b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimResponse.java new file mode 100644 index 0000000000..1870277deb --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantLicenseClaimResponse.java @@ -0,0 +1,8 @@ +package org.thingsboard.server.service.community_grant; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +/** {@code secret} is set only once {@code activated} is true. */ +@JsonIgnoreProperties(ignoreUnknown = true) +public record CommunityGrantLicenseClaimResponse(boolean activated, String secret) { +} diff --git a/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantPoller.java b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantPoller.java index 0b192ad224..a19f4e3163 100644 --- a/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantPoller.java +++ b/application/src/main/java/org/thingsboard/server/service/community_grant/CommunityGrantPoller.java @@ -358,9 +358,7 @@ public class CommunityGrantPoller extends TbApplicationEventListener client.claimLicense(TOKEN)) + .isInstanceOf(CommunityGrantLicenseClaimRefusedException.class); + mockServer.verify(); + } + + @Test + void testClaimLicenseLeavesARetryableAnswerToTheCaller() { + mockServer.expect(requestTo(BASE_URL + "/api/noauth/freeLicense/claim")) + .andRespond(withStatus(HttpStatus.TOO_MANY_REQUESTS)); + mockServer.expect(requestTo(BASE_URL + "/api/noauth/freeLicense/claim")) + .andRespond(withServerError()); + + assertThatThrownBy(() -> client.claimLicense(TOKEN)) + .isInstanceOf(HttpStatusCodeException.class) + .isNotInstanceOf(CommunityGrantLicenseClaimRefusedException.class); + assertThatThrownBy(() -> client.claimLicense(TOKEN)) + .isInstanceOf(HttpStatusCodeException.class) + .isNotInstanceOf(CommunityGrantLicenseClaimRefusedException.class); + mockServer.verify(); + } + @Test void testDownloadChecker() { mockServer.expect(requestTo(BASE_URL + "/api/noauth/communityGrant/checker?os=linux&arch=amd64")) diff --git a/dao/src/main/java/org/thingsboard/server/dao/subscription/TbClusterStore.java b/dao/src/main/java/org/thingsboard/server/dao/subscription/TbClusterStore.java index 44c26e57e9..e8c480447c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/subscription/TbClusterStore.java +++ b/dao/src/main/java/org/thingsboard/server/dao/subscription/TbClusterStore.java @@ -11,7 +11,10 @@ import org.springframework.stereotype.Component; import java.util.Optional; import java.util.UUID; -/** Accessor for the single {@code tb_cluster} row: the cluster id and the license portal claim token. */ +/** + * Accessor for the single {@code tb_cluster} row: the cluster id, the license portal claim token and the + * license key the portal granted. + */ @Component @Profile("!install") @RequiredArgsConstructor @@ -25,6 +28,14 @@ public class TbClusterStore { rs -> rs.next() ? Optional.ofNullable(rs.getObject("cluster_id", UUID.class)) : Optional.empty()); } + public Optional getLicenseSecret() { + return getColumn("license_secret"); + } + + public void saveLicenseSecret(String secret) { + updateColumn("license_secret", secret, "license secret"); + } + public Optional getLicenseClaimToken() { return getColumn("license_claim_token"); } diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index 711eca9c53..7b86f84231 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS tb_cluster ( cluster_id uuid NOT NULL, license_claim_token varchar, + license_secret varchar, CONSTRAINT tb_cluster_pkey PRIMARY KEY (cluster_id) ); diff --git a/dao/src/test/java/org/thingsboard/server/dao/subscription/TbClusterStoreTest.java b/dao/src/test/java/org/thingsboard/server/dao/subscription/TbClusterStoreTest.java index a2002079c8..502f04de8c 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/subscription/TbClusterStoreTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/subscription/TbClusterStoreTest.java @@ -71,6 +71,33 @@ public class TbClusterStoreTest extends AbstractServiceTest { assertThat(tbClusterStore.getLicenseClaimToken()).contains("token-two"); } + @Test + public void testSaveAndReadLicenseSecret() { + assertThat(tbClusterStore.getLicenseSecret()).isEmpty(); + tbClusterStore.saveLicenseSecret("secret-one"); + assertThat(tbClusterStore.getLicenseSecret()).contains("secret-one"); + tbClusterStore.saveLicenseSecret("secret-two"); + assertThat(tbClusterStore.getLicenseSecret()).contains("secret-two"); + } + + @Test + public void testLicenseSecretAndClaimTokenAreStoredApart() { + tbClusterStore.saveLicenseSecret("secret-one"); + tbClusterStore.saveLicenseClaimToken("token-one"); + tbClusterStore.clearLicenseClaimToken("token-one"); + + assertThat(tbClusterStore.getLicenseSecret()).contains("secret-one"); + assertThat(tbClusterStore.getLicenseClaimToken()).isEmpty(); + } + + @Test + public void testSaveLicenseSecretFailsWhenClusterRowIsMissing() { + jdbcTemplate.update("DELETE FROM tb_cluster"); + assertThatThrownBy(() -> tbClusterStore.saveLicenseSecret("secret-one")) + .isInstanceOf(IllegalStateException.class); + assertThat(tbClusterStore.getLicenseSecret()).isEqualTo(Optional.empty()); + } + @Test public void testClearLicenseClaimToken() { tbClusterStore.saveLicenseClaimToken("token-one");