Skip to content

Commit

Permalink
Add clippy.toml with forbidden methods & fix SystemTime usages (#2882)
Browse files Browse the repository at this point in the history
## Motivation and Context
- #2087 

## Description
- remove lingering usages of SystemTime::now()
- ensure they don't return by adding a clippy.toml

## Testing
- CI
- Ran the webassembly example

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: John DiSanti <jdisanti@amazon.com>
  • Loading branch information
rcoh and jdisanti authored Jul 28, 2023
1 parent cf8df40 commit a0b60ed
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions aws/rust-runtime/aws-config/clippy.toml
6 changes: 5 additions & 1 deletion aws/rust-runtime/aws-inlineable/src/presigning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ impl PresigningConfigBuilder {
return Err(ErrorKind::ExpiresInDurationTooLong.into());
}
Ok(PresigningConfig {
start_time: self.start_time.unwrap_or_else(SystemTime::now),
start_time: self.start_time.unwrap_or_else(
// This usage is OK—customers can easily override this.
#[allow(clippy::disallowed_methods)]
SystemTime::now,
),
expires_in,
})
}
Expand Down
4 changes: 3 additions & 1 deletion aws/rust-runtime/aws-runtime/src/request_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ mod tests {
use super::RequestInfoInterceptor;
use crate::request_info::RequestPairs;
use aws_smithy_http::body::SdkBody;
use aws_smithy_runtime_api::client::interceptors::context::{Input, InterceptorContext};
use aws_smithy_runtime_api::client::interceptors::context::Input;
use aws_smithy_runtime_api::client::interceptors::context::InterceptorContext;
use aws_smithy_runtime_api::client::interceptors::Interceptor;
use aws_smithy_runtime_api::client::runtime_components::RuntimeComponentsBuilder;
use aws_smithy_types::config_bag::{ConfigBag, Layer};
use aws_smithy_types::retry::RetryConfig;
use aws_smithy_types::timeout::TimeoutConfig;

use http::HeaderValue;
use std::time::Duration;

Expand Down
11 changes: 8 additions & 3 deletions aws/rust-runtime/aws-runtime/src/service_clock_skew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
use aws_smithy_types::config_bag::{ConfigBag, Storable, StoreReplace};
use aws_smithy_types::date_time::Format;
use aws_smithy_types::DateTime;
use std::time::{Duration, SystemTime};
use std::time::Duration;

/// Amount of clock skew between the client and the service.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -72,10 +72,15 @@ impl Interceptor for ServiceClockSkewInterceptor {
fn modify_before_deserialization(
&self,
ctx: &mut BeforeDeserializationInterceptorContextMut<'_>,
_runtime_components: &RuntimeComponents,
runtime_components: &RuntimeComponents,
cfg: &mut ConfigBag,
) -> Result<(), BoxError> {
let time_received = DateTime::from(SystemTime::now());
let time_received = DateTime::from(
runtime_components
.time_source()
.ok_or("a time source is required (service clock skew)")?
.now(),
);
let time_sent = match extract_time_sent_from_response(ctx) {
Ok(time_sent) => time_sent,
Err(e) => {
Expand Down
3 changes: 2 additions & 1 deletion aws/rust-runtime/aws-sig-auth/src/event_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

// TODO(enableNewSmithyRuntimeCleanup): Remove this blanket allow once the old implementations are deleted
// this code is dead
#![allow(deprecated)]
#![allow(clippy::disallowed_methods)]

use crate::middleware::Signature;
use aws_credential_types::Credentials;
Expand Down
2 changes: 2 additions & 0 deletions aws/rust-runtime/aws-sig-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

// TODO(enableNewSmithyRuntimeCleanup): Deprecate this crate and replace it with empty contents. Remove references to it in the code generator.

#![allow(clippy::derive_partial_eq_without_eq)]

//! AWS Signature Authentication Package
Expand Down
1 change: 1 addition & 0 deletions aws/rust-runtime/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class EndpointsCredentialsTest {
.credentials_provider(#{Credentials}::for_tests())
.build();
let client = $moduleName::Client::from_conf(conf);
let _ = client.custom_auth().send().await;
let _ = dbg!(client.custom_auth().send().await);
let req = rcvr.expect_request();
let auth_header = req.headers().get("AUTHORIZATION").unwrap().to_str().unwrap();
assert!(auth_header.contains("/region-custom-auth/name-custom-auth/aws4_request"), "{}", auth_header);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class InvocationIdDecoratorTest {
let client = $moduleName::Client::from_conf(config);
let _ = client.some_operation().send().await;
let _ = dbg!(client.some_operation().send().await);
let request = rx.expect_request();
assert_eq!("custom", request.headers().get("amz-sdk-invocation-id").unwrap());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun awsSdkIntegrationTest(
clientIntegrationTest(
model,
IntegrationTestParams(
cargoCommand = "cargo test --features test-util",
runtimeConfig = AwsTestRuntimeConfig,
additionalSettings = ObjectNode.builder().withMember(
"customizationConfig",
Expand Down
2 changes: 2 additions & 0 deletions aws/sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ tasks.register("generateCargoWorkspace") {
doFirst {
outputDir.mkdirs()
outputDir.resolve("Cargo.toml").writeText(generateCargoWorkspace(awsServices))
rootProject.rootDir.resolve("clippy-root.toml").copyTo(outputDir.resolve("clippy.toml"))
}
inputs.property("servicelist", awsServices.moduleNames.toString())
if (awsServices.examples.isNotEmpty()) {
Expand All @@ -347,6 +348,7 @@ tasks.register("generateCargoWorkspace") {
inputs.dir(test.path)
}
outputs.file(outputDir.resolve("Cargo.toml"))
outputs.file(outputDir.resolve("clippy.toml"))
outputs.upToDateWhen { false }
}

Expand Down
9 changes: 9 additions & 0 deletions clippy-root.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# this file is named `clippy-root.toml` so it isn't picked up automagically. Clippy
# will search up the filesystem for a clippy.toml and this causes problems with tools.
disallowed-methods = [
# fully qualified function/method name:
"std::time::SystemTime::now",
"std::time::SystemTime::elapsed",
"std::time::Instant::now",
"std::time::Instant::elapsed"
]
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class TimeSourceCustomization(codegenContext: ClientCodegenContext) : ConfigCust
private val codegenScope = arrayOf(
*preludeScope,
"SharedTimeSource" to RuntimeType.smithyAsync(codegenContext.runtimeConfig).resolve("time::SharedTimeSource"),
"StaticTimeSource" to RuntimeType.smithyAsync(codegenContext.runtimeConfig).resolve("time::StaticTimeSource"),
"UNIX_EPOCH" to RuntimeType.std.resolve("time::UNIX_EPOCH"),
"Duration" to RuntimeType.std.resolve("time::Duration"),
)

override fun section(section: ServiceConfig) =
Expand Down Expand Up @@ -129,6 +132,18 @@ class TimeSourceCustomization(codegenContext: ClientCodegenContext) : ConfigCust
}
}

is ServiceConfig.DefaultForTests -> {
rustTemplate(
"""
${section.configBuilderRef}
.set_time_source(#{Some}(#{SharedTimeSource}::new(
#{StaticTimeSource}::new(#{UNIX_EPOCH} + #{Duration}::from_secs(1234567890)))
));
""",
*codegenScope,
)
}

else -> emptySection
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data class IntegrationTestParams(
val additionalSettings: ObjectNode = ObjectNode.builder().build(),
val overrideTestDir: File? = null,
val command: ((Path) -> Unit)? = null,
val cargoCommand: String? = null,
)

/**
Expand All @@ -40,6 +41,6 @@ fun codegenIntegrationTest(model: Model, params: IntegrationTestParams, invokePl
)
invokePlugin(ctx)
ctx.fileManifest.printGeneratedFiles()
params.command?.invoke(testDir) ?: "cargo test".runCommand(testDir, environment = mapOf("RUSTFLAGS" to "-D warnings"))
params.command?.invoke(testDir) ?: (params.cargoCommand ?: "cargo test").runCommand(testDir, environment = mapOf("RUSTFLAGS" to "-D warnings"))
return testDir
}
2 changes: 2 additions & 0 deletions rust-runtime/aws-smithy-async/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl SystemTimeSource {

impl TimeSource for SystemTimeSource {
fn now(&self) -> SystemTime {
// this is the one OK usage
#[allow(clippy::disallowed_methods)]
SystemTime::now()
}
}
Expand Down
1 change: 1 addition & 0 deletions rust-runtime/clippy.toml

0 comments on commit a0b60ed

Please sign in to comment.