Skip to content

Commit

Permalink
Made signer take a signing config by value, made sure stream seek cle…
Browse files Browse the repository at this point in the history
…ars stream state, updated body signing api. (#78)
  • Loading branch information
JonathanHenson authored Nov 27, 2019
1 parent 1d1a2be commit bef3faf
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion aws-common-runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(AWS_C_CAL_SHA "v0.2.0")
include(BuildAwsCCal)

set(AWS_C_AUTH_URL "https://github.com/awslabs/aws-c-auth.git")
set(AWS_C_AUTH_SHA "v0.3.2")
set(AWS_C_AUTH_SHA "v0.3.3")
include(BuildAwsCAuth)

add_dependencies(AwsCCompression AwsCCommon)
Expand Down
2 changes: 1 addition & 1 deletion include/aws/crt/auth/Signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace Aws

virtual bool SignRequest(
const std::shared_ptr<Aws::Crt::Http::HttpRequest> &request,
const std::shared_ptr<ISigningConfig> &config,
const ISigningConfig &config,
const OnHttpRequestSigningComplete &completionCallback) = 0;

/**
Expand Down
13 changes: 10 additions & 3 deletions include/aws/crt/auth/Sigv4Signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ namespace Aws
Count = AWS_SIGNING_ALGORITHM_COUNT
};

enum class BodySigningType
{
NoSigning = AWS_BODY_SIGNING_OFF,
SignBody = AWS_BODY_SIGNING_ON,
UnsignedPayload = AWS_BODY_SIGNING_UNSIGNED_PAYLOAD
};

using ShouldSignParameterCb = bool (*)(const Crt::ByteCursor *, void *);

/**
Expand Down Expand Up @@ -137,13 +144,13 @@ namespace Aws
* Gets whether or not the signer should add the x-amz-content-sha256 header (with appropriate value) to
* the canonical request.
*/
bool GetSignBody() const noexcept;
BodySigningType GetBodySigningType() const noexcept;

/**
* Sets whether or not the signer should add the x-amz-content-sha256 header (with appropriate value) to
* the canonical request.
*/
void SetSignBody(bool signBody) noexcept;
void SetBodySigningType(BodySigningType bodysigningType) noexcept;

/**
* Get the credentials provider to use for signing.
Expand Down Expand Up @@ -180,7 +187,7 @@ namespace Aws
*/
virtual bool SignRequest(
const std::shared_ptr<Aws::Crt::Http::HttpRequest> &request,
const std::shared_ptr<ISigningConfig> &config,
const ISigningConfig &config,
const OnHttpRequestSigningComplete &completionCallback) override;

private:
Expand Down
21 changes: 12 additions & 9 deletions source/auth/Sigv4Signing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Aws

SetSigningAlgorithm(SigningAlgorithm::SigV4Header);
SetShouldNormalizeUriPath(true);
SetSignBody(true);
SetBodySigningType(BodySigningType::SignBody);
SetSigningTimepoint(DateTime::Now());
m_config.config_type = AWS_SIGNING_CONFIG_AWS;
}
Expand Down Expand Up @@ -104,9 +104,15 @@ namespace Aws
m_config.should_sign_param = shouldSignParameterCb;
}

bool AwsSigningConfig::GetSignBody() const noexcept { return m_config.sign_body; }
BodySigningType AwsSigningConfig::GetBodySigningType() const noexcept
{
return static_cast<BodySigningType>(m_config.body_signing_type);
}

void AwsSigningConfig::SetSignBody(bool signBody) noexcept { m_config.sign_body = signBody; }
void AwsSigningConfig::SetBodySigningType(BodySigningType bodysigningType) noexcept
{
m_config.body_signing_type = static_cast<enum aws_body_signing_config_type>(bodysigningType);
}

const std::shared_ptr<ICredentialsProvider> &AwsSigningConfig::GetCredentialsProvider() const noexcept
{
Expand Down Expand Up @@ -138,8 +144,6 @@ namespace Aws
Allocator *Alloc;
ScopedResource<struct aws_signable> Signable;
OnHttpRequestSigningComplete OnRequestSigningComplete;
// just hold on to this for lifetime, we don't actually use it.
std::shared_ptr<ISigningConfig> Config;
std::shared_ptr<Http::HttpRequest> Request;
};

Expand All @@ -159,16 +163,16 @@ namespace Aws

bool Sigv4HttpRequestSigner::SignRequest(
const std::shared_ptr<Aws::Crt::Http::HttpRequest> &request,
const std::shared_ptr<ISigningConfig> &config,
const ISigningConfig &config,
const OnHttpRequestSigningComplete &completionCallback)
{
if (config->GetType() != SigningConfigType::Aws)
if (config.GetType() != SigningConfigType::Aws)
{
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return false;
}

auto awsSigningConfig = static_cast<const AwsSigningConfig *>(config.get());
auto awsSigningConfig = static_cast<const AwsSigningConfig *>(&config);

if (!awsSigningConfig->GetCredentialsProvider())
{
Expand All @@ -184,7 +188,6 @@ namespace Aws
}

signerCallbackData->Alloc = m_allocator;
signerCallbackData->Config = config;
signerCallbackData->OnRequestSigningComplete = completionCallback;
signerCallbackData->Request = request;
signerCallbackData->Signable = ScopedResource<struct aws_signable>(
Expand Down
1 change: 1 addition & 0 deletions source/io/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static int s_aws_input_stream_cpp_seek(
enum aws_stream_seek_basis basis)
{
aws_input_stream_cpp_impl *impl = static_cast<aws_input_stream_cpp_impl *>(stream->impl);
impl->stream->clear();
impl->stream->seekg(Aws::Crt::Io::IStream::off_type(offset), s_stream_seek_basis_to_seekdir(basis));

return AWS_OP_SUCCESS;
Expand Down
8 changes: 4 additions & 4 deletions source/iot/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Aws
signerConfig->SetRegion(signingRegionCopy);
signerConfig->SetService(serviceNameCopy);
signerConfig->SetSigningAlgorithm(Crt::Auth::SigningAlgorithm::SigV4QueryParam);
signerConfig->SetSignBody(false);
signerConfig->SetBodySigningType(Crt::Auth::BodySigningType::NoSigning);
signerConfig->SetShouldSignHeadersCallback(s_blackListHeadersFromSigning);
signerConfig->SetCredentialsProvider(credsProviderRef);

Expand All @@ -79,7 +79,7 @@ namespace Aws
signerConfig->SetRegion(signingRegionCopy);
signerConfig->SetService(serviceNameCopy);
signerConfig->SetSigningAlgorithm(Crt::Auth::SigningAlgorithm::SigV4QueryParam);
signerConfig->SetSignBody(false);
signerConfig->SetBodySigningType(Crt::Auth::BodySigningType::NoSigning);
signerConfig->SetShouldSignHeadersCallback(s_blackListHeadersFromSigning);
signerConfig->SetCredentialsProvider(credsProviderRef);

Expand Down Expand Up @@ -301,7 +301,7 @@ namespace Aws

auto signerConfig = websocketConfig.CreateSigningConfigCb();

websocketConfig.Signer->SignRequest(req, signerConfig, signingComplete);
websocketConfig.Signer->SignRequest(req, *signerConfig, signingComplete);
};

return MqttClientConnectionConfig(
Expand Down Expand Up @@ -360,4 +360,4 @@ namespace Aws
return newConnection;
}
} // namespace Iot
} // namespace Aws
} // namespace Aws
10 changes: 5 additions & 5 deletions tests/Sigv4SigningTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ static int s_Sigv4SigningTestSimple(struct aws_allocator *allocator, void *ctx)

auto request = s_MakeDummyRequest(allocator);

auto signingConfig = Aws::Crt::MakeShared<AwsSigningConfig>(allocator, allocator);
signingConfig->SetSigningTimepoint(Aws::Crt::DateTime());
signingConfig->SetRegion("test");
signingConfig->SetService("service");
signingConfig->SetCredentialsProvider(provider);
AwsSigningConfig signingConfig(allocator);
signingConfig.SetSigningTimepoint(Aws::Crt::DateTime());
signingConfig.SetRegion("test");
signingConfig.SetService("service");
signingConfig.SetCredentialsProvider(provider);

SignWaiter waiter;

Expand Down

0 comments on commit bef3faf

Please sign in to comment.