Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Multiple License Products when using Unity Licensing Server #282

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ inputs:
required: false
default: ''
description: 'Url to a unity license server for acquiring floating licenses.'
unityLicensingProductIds:
required: false
default: ''
description:
'Comma separated list of license product identifiers to request licenses for from the license server. Not setting
this will request the default license product configured on the licensing server. Only applicable if
unityLicensingServer is set.'
containerRegistryRepository:
required: false
default: 'unityci/editor'
Expand Down
11 changes: 8 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/unity-config/services-config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"enableEntitlementLicensing": true,
"enableFloatingApi": true,
"clientConnectTimeoutSec": 5,
"clientHandshakeTimeoutSec": 10
"clientHandshakeTimeoutSec": 10,
"toolset": "%LICENSE_PRODUCT_IDS%"
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function run() {
dockerMemoryLimit,
dockerIsolationMode,
unityLicensingServer,
unityLicensingProductIds,
runAsHostUser,
containerRegistryRepository,
containerRegistryImageVersion,
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function run() {
dockerMemoryLimit,
dockerIsolationMode,
unityLicensingServer,
unityLicensingProductIds,
runAsHostUser,
unitySerial,
...runnerContext,
Expand Down
6 changes: 5 additions & 1 deletion src/model/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const Docker = {
let runCommand = '';

if (parameters.unityLicensingServer !== '') {
LicensingServerSetup.Setup(parameters.unityLicensingServer, parameters.actionFolder);
LicensingServerSetup.Setup(
parameters.unityLicensingServer,
parameters.actionFolder,
parameters.unityLicensingProductIds,
);
Comment on lines +37 to +41
Copy link

@coderabbitai coderabbitai bot Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding type safety and validation for unityLicensingProductIds.

While the change correctly implements the new parameter, consider adding validation to ensure the product IDs are in the correct format before passing them to Setup.

Consider wrapping the Setup call with validation:

      LicensingServerSetup.Setup(
        parameters.unityLicensingServer,
        parameters.actionFolder,
-       parameters.unityLicensingProductIds,
+       validateProductIds(parameters.unityLicensingProductIds),
      );

+ function validateProductIds(ids: string): string {
+   if (!ids) return '';
+   const productIds = ids.split(',').map(id => id.trim());
+   if (productIds.some(id => !/^[\w-]+$/.test(id))) {
+     throw new Error('Invalid product ID format. Product IDs should only contain alphanumeric characters, hyphens and underscores.');
+   }
+   return productIds.join(',');
+ }

Committable suggestion was skipped due to low confidence.

Copy link
Member

@GabLeRoux GabLeRoux Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this suggestion as I don't have much details about unityLicensingProductIds's actual format, but it sounds a like a good idea to do input validation.

On the other hand, we're not doing validation for other parameters so this might be out of scope. Feel free to discard.

edit: I've deleted this learning as not relevant. Suggesting input validation is usually good

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GabLeRoux, understood. Since input validation for other parameters is not currently implemented and might be out of scope for this PR, it's acceptable to discard this suggestion. Thank you for clarifying.


✏️ Learnings added
Learnt from: GabLeRoux
PR: game-ci/unity-test-runner#282
File: src/model/docker.ts:37-41
Timestamp: 2024-10-27T16:14:24.321Z
Learning: Input validation for parameters such as `unityLicensingProductIds` is not currently implemented in the codebase, so suggesting to add validation might be out of scope during code reviews.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

}

switch (process.platform) {
Expand Down
1 change: 1 addition & 0 deletions src/model/image-environment-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ImageEnvironmentFactory {
name: 'UNITY_LICENSING_SERVER',
value: parameters.unityLicensingServer,
},
{ name: 'UNITY_LICENSING_PRODUCT_IDS', value: parameters.unityLicensingProductIds },
{ name: 'UNITY_VERSION', value: parameters.editorVersion },
{
name: 'USYM_UPLOAD_AUTH_TOKEN',
Expand Down
2 changes: 2 additions & 0 deletions src/model/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Input {
const customImage = getInput('customImage') || '';
const rawProjectPath = getInput('projectPath') || '.';
const unityLicensingServer = getInput('unityLicensingServer') || '';
const unityLicensingProductIds = getInput('unityLicensingProductIds') || '';
const unityLicense = getInput('unityLicense') || (process.env['UNITY_LICENSE'] ?? '');
let unitySerial = process.env['UNITY_SERIAL'] ?? '';
const customParameters = getInput('customParameters') || '';
Expand Down Expand Up @@ -239,6 +240,7 @@ class Input {
dockerMemoryLimit,
dockerIsolationMode,
unityLicensingServer,
unityLicensingProductIds,
runAsHostUser,
containerRegistryRepository,
containerRegistryImageVersion,
Expand Down
7 changes: 6 additions & 1 deletion src/model/licensing-server-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import fs from 'fs';

class LicensingServerSetup {
public static Setup(unityLicensingServer, actionFolder: string) {
public static Setup(
unityLicensingServer,
actionFolder: string,
unityLicensingProductIds: string,
) {
const servicesConfigPath = `${actionFolder}/unity-config/services-config.json`;
const servicesConfigPathTemplate = `${servicesConfigPath}.template`;
if (!fs.existsSync(servicesConfigPathTemplate)) {
Expand All @@ -13,6 +17,7 @@ class LicensingServerSetup {

let servicesConfig = fs.readFileSync(servicesConfigPathTemplate).toString();
servicesConfig = servicesConfig.replace('%URL%', unityLicensingServer);
servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', unityLicensingProductIds);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add input validation and maintain backward compatibility.

The current implementation has potential security and compatibility concerns:

  1. Direct string replacement without sanitization
  2. No handling of empty values
  3. No validation of product ID format

Consider applying these changes:

+    // Validate and sanitize product IDs
+    const sanitizedProductIds = unityLicensingProductIds
+      ?.split(',')
+      .map(id => id.trim())
+      .filter(id => /^[\w-]+$/.test(id))  // Allow only alphanumeric chars and hyphens
+      .join(',');
+
+    // Only replace if valid product IDs are provided
+    if (sanitizedProductIds) {
+      servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', sanitizedProductIds);
+    } else {
+      // Maintain backward compatibility by using empty string
+      servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', '');
+    }
-    servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', unityLicensingProductIds);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', unityLicensingProductIds);
// Validate and sanitize product IDs
const sanitizedProductIds = unityLicensingProductIds
?.split(',')
.map(id => id.trim())
.filter(id => /^[\w-]+$/.test(id)) // Allow only alphanumeric chars and hyphens
.join(',');
// Only replace if valid product IDs are provided
if (sanitizedProductIds) {
servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', sanitizedProductIds);
} else {
// Maintain backward compatibility by using empty string
servicesConfig = servicesConfig.replace('%LICENSE_PRODUCT_IDS%', '');
}

fs.writeFileSync(servicesConfigPath, servicesConfig);
}
}
Expand Down