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

Enable creation of strong names for .NET assemblies. #643

Merged
merged 10 commits into from Sep 11, 2018
3 changes: 3 additions & 0 deletions buildspec.yaml
Expand Up @@ -4,6 +4,9 @@ phases:
install:
commands:
- /bin/bash ./install.sh
pre_build:
commands:
- /bin/bash ./fetch-dotnet-snk.sh
build:
commands:
- /bin/bash ./build.sh
Expand Down
28 changes: 28 additions & 0 deletions fetch-dotnet-snk.sh
@@ -0,0 +1,28 @@
#!/bin/bash
set -euo pipefail

# This script retrieves the .snk file needed to create strong names for .NET assemblies.

sudo apt install jq -y

echo "Retrieving SNK..."
ROLE=$(aws sts assume-role --region us-east-2 --role-arn ${DOTNET_STRONG_NAME_ROLE_ARN:-} --role-session-name "cdk-dotnet-snk")
Copy link
Contributor

Choose a reason for hiding this comment

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

region pass in?

export AWS_ACCESS_KEY_ID=$(echo $ROLE | jq -r .Credentials.AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo $ROLE | jq -r .Credentials.SecretAccessKey)
export AWS_SESSION_TOKEN=$(echo $ROLE | jq .Credentials.SessionToken)

SNK_SECRET=$(aws secretsmanager get-secret-value --region us-east-2 --secret-id ${DOTNET_STRONG_NAME_SECRET_ID:-})
TMP_DIR=$(mktemp -d)
TMP_KEY="$TMP_DIR/key.snk"
echo $SNK_SECRET | jq -r .SecretBinary | base64 --decode > $TMP_KEY

for PACKAGE_PATH in packages/@aws-cdk/*; do
JSII_PROPERTY=$(cat "$PACKAGE_PATH/package.json" | jq -r .jsii)
if [ -z $JSII_PROPERTY ]; then
continue
fi

cp $TMP_KEY $PACKAGE_PATH
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of copying this file everywhere, maybe the .NET build can consult an environment variable for it's location?

Copy link
Contributor Author

@mpiroc mpiroc Aug 30, 2018

Choose a reason for hiding this comment

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

  • Using an environment variable is an extra level of indirection that we don't need (we already have too many [levels of indirection]!). There's no cost to copying the file everywhere.
  • I'm trying to minimize any necessary changes if we ever decide to move away from a monorepo. While an environment variable would still work with one package per repo, it seems like overkill.

Copy link
Contributor Author

@mpiroc mpiroc Aug 30, 2018

Choose a reason for hiding this comment

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

Another issue with using an environment variable is: How would it be set? The temporary directory is created by fetch-dotnet-snk.sh, but build.sh is a sibling process of fetch-dotnet-snk.sh, not a descendant. A process can't modify its parent's environment variables.

done

rm -rf $TMP_DIR
27 changes: 27 additions & 0 deletions tools/pkglint/lib/rules.ts
Expand Up @@ -256,6 +256,33 @@ export class JSIIDotNetNamespaceIsRequired extends ValidationRule {
}
}

/**
* Strong-naming all .NET assemblies is required.
*/
export class JSIIDotNetStrongNameIsRequired extends ValidationRule {
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }

const signAssembly = deepGet(pkg.json, ['jsii', 'targets', 'dotnet', 'signAssembly']) as boolean | undefined;
const signAssemblyExpected = true;
if (signAssembly !== signAssemblyExpected) {
pkg.report({
message: `.NET packages must have strong-name signing enabled.`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'dotnet', 'signAssembly'], signAssemblyExpected)
});
}

const assemblyOriginatorKeyFile = deepGet(pkg.json, ['jsii', 'targets', 'dotnet', 'assemblyOriginatorKeyFile']) as string | undefined;
const assemblyOriginatorKeyFileExpected = "../../key.snk";
if (assemblyOriginatorKeyFile !== assemblyOriginatorKeyFileExpected) {
pkg.report({
message: `.NET packages must use the strong name key fetched by fetch-dotnet-snk.sh`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'dotnet', 'assemblyOriginatorKeyFile'], assemblyOriginatorKeyFileExpected)
});
}
}
}

/**
* The package must depend on cdk-build-tools
*/
Expand Down