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

Add validation context provider #4264

Merged
merged 7 commits into from
Aug 29, 2018
Merged
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
1 change: 1 addition & 0 deletions include/envoy/secret/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ envoy_cc_library(
deps = [
":secret_callbacks_interface",
"//include/envoy/common:callback",
"//include/envoy/ssl:certificate_validation_context_config_interface",
"//include/envoy/ssl:tls_certificate_config_interface",
],
)
Expand Down
18 changes: 18 additions & 0 deletions include/envoy/secret/secret_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ class SecretManager {
virtual TlsCertificateConfigProviderSharedPtr
findStaticTlsCertificateProvider(const std::string& name) const PURE;

/**
* @param name a name of the static CertificateValidationContextConfigProviderSharedPtr.
* @return the CertificateValidationContextConfigProviderSharedPtr. Returns nullptr
* if the static certificate validation context is not found.
*/
virtual CertificateValidationContextConfigProviderSharedPtr
findStaticCertificateValidationContextProvider(const std::string& name) const PURE;

/**
* @param tls_certificate the protobuf config of the TLS certificate.
* @return a TlsCertificateConfigProviderSharedPtr created from tls_certificate.
*/
virtual TlsCertificateConfigProviderSharedPtr createInlineTlsCertificateProvider(
const envoy::api::v2::auth::TlsCertificate& tls_certificate) PURE;

/**
* @param tls_certificate the protobuf config of the certificate validation context.
* @return a CertificateValidationContextConfigProviderSharedPtr created from
* certificate_validation_context.
*/
virtual CertificateValidationContextConfigProviderSharedPtr
createInlineCertificateValidationContextProvider(
const envoy::api::v2::auth::CertificateValidationContext& certificate_validation_context)
PURE;
};

} // namespace Secret
Expand Down
6 changes: 6 additions & 0 deletions include/envoy/secret/secret_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "envoy/common/callback.h"
#include "envoy/common/pure.h"
#include "envoy/ssl/certificate_validation_context_config.h"
#include "envoy/ssl/tls_certificate_config.h"

namespace Envoy {
Expand Down Expand Up @@ -34,5 +35,10 @@ template <class SecretType> class SecretProvider {
typedef SecretProvider<Ssl::TlsCertificateConfig> TlsCertificateConfigProvider;
typedef std::shared_ptr<TlsCertificateConfigProvider> TlsCertificateConfigProviderSharedPtr;

typedef SecretProvider<Ssl::CertificateValidationContextConfig>
CertificateValidationContextConfigProvider;
typedef std::shared_ptr<CertificateValidationContextConfigProvider>
CertificateValidationContextConfigProviderSharedPtr;

} // namespace Secret
} // namespace Envoy
6 changes: 6 additions & 0 deletions include/envoy/ssl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ envoy_cc_library(
name = "context_config_interface",
hdrs = ["context_config.h"],
deps = [
":certificate_validation_context_config_interface",
":tls_certificate_config_interface",
],
)
Expand All @@ -40,3 +41,8 @@ envoy_cc_library(
name = "tls_certificate_config_interface",
hdrs = ["tls_certificate_config.h"],
)

envoy_cc_library(
name = "certificate_validation_context_config_interface",
hdrs = ["certificate_validation_context_config.h"],
)
61 changes: 61 additions & 0 deletions include/envoy/ssl/certificate_validation_context_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <string>
#include <vector>

#include "envoy/common/pure.h"

namespace Envoy {
namespace Ssl {

class CertificateValidationContextConfig {
public:
virtual ~CertificateValidationContextConfig() {}

/**
* @return The CA certificate to use for peer validation.
*/
virtual const std::string& caCert() const PURE;

/**
* @return Path of the CA certificate to use for peer validation or "<inline>"
* if the CA certificate was inlined.
*/
virtual const std::string& caCertPath() const PURE;

/**
* @return The CRL to check if a cert is revoked.
*/
virtual const std::string& certificateRevocationList() const PURE;

/**
* @return Path of the certificate revocation list, or "<inline>" if the CRL
* was inlined.
*/
virtual const std::string& certificateRevocationListPath() const PURE;

/**
* @return The subject alt names to be verified, if enabled. Otherwise, ""
*/
virtual const std::vector<std::string>& verifySubjectAltNameList() const PURE;

/**
* @return A list of a hex-encoded SHA-256 certificate hashes to be verified.
*/
virtual const std::vector<std::string>& verifyCertificateHashList() const PURE;

/**
* @return A list of a hex-encoded SHA-256 SPKI hashes to be verified.
*/
virtual const std::vector<std::string>& verifyCertificateSpkiList() const PURE;

/**
* @return whether to ignore expired certificates (both too new and too old).
*/
virtual bool allowExpiredCertificate() const PURE;
};

typedef std::unique_ptr<CertificateValidationContextConfig> CertificateValidationContextConfigPtr;

} // namespace Ssl
} // namespace Envoy
42 changes: 3 additions & 39 deletions include/envoy/ssl/context_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>

#include "envoy/common/pure.h"
#include "envoy/ssl/certificate_validation_context_config.h"
#include "envoy/ssl/tls_certificate_config.h"

namespace Envoy {
Expand Down Expand Up @@ -39,52 +40,15 @@ class ContextConfig {
*/
virtual const std::string& ecdhCurves() const PURE;

/**
* @return The CA certificate to use for peer validation.
*/
virtual const std::string& caCert() const PURE;

/**
* @return Path of the CA certificate to use for peer validation or "<inline>"
* if the CA certificate was inlined.
*/
virtual const std::string& caCertPath() const PURE;

/**
* @return The CRL to check if a cert is revoked.
*/
virtual const std::string& certificateRevocationList() const PURE;

/**
* @return Path of the certificate revocation list, or "<inline>" if the CRL
* was inlined.
*/
virtual const std::string& certificateRevocationListPath() const PURE;

/**
* @return TlsCertificateConfig the certificate config used to identify the local side.
*/
virtual const TlsCertificateConfig* tlsCertificate() const PURE;

/**
* @return The subject alt names to be verified, if enabled. Otherwise, ""
*/
virtual const std::vector<std::string>& verifySubjectAltNameList() const PURE;

/**
* @return A list of a hex-encoded SHA-256 certificate hashes to be verified.
*/
virtual const std::vector<std::string>& verifyCertificateHashList() const PURE;

/**
* @return A list of a hex-encoded SHA-256 SPKI hashes to be verified.
*/
virtual const std::vector<std::string>& verifyCertificateSpkiList() const PURE;

/**
* @return whether to ignore expired certificates (both too new and too old).
* @return CertificateValidationContextConfig the certificate validation context config.
*/
virtual bool allowExpiredCertificate() const PURE;
virtual const CertificateValidationContextConfig* certificateValidationContext() const PURE;

/**
* @return The minimum TLS protocol version to negotiate.
Expand Down
1 change: 1 addition & 0 deletions source/common/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Logger {
FUNCTION(router) \
FUNCTION(runtime) \
FUNCTION(stats) \
FUNCTION(secret) \
FUNCTION(testing) \
FUNCTION(thrift) \
FUNCTION(tracing) \
Expand Down
1 change: 1 addition & 0 deletions source/common/secret/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ envoy_cc_library(
hdrs = ["secret_provider_impl.h"],
deps = [
"//include/envoy/secret:secret_provider_interface",
"//source/common/ssl:certificate_validation_context_config_impl_lib",
"//source/common/ssl:tls_certificate_config_impl_lib",
"@envoy_api//envoy/api/v2/auth:cert_cc",
],
Expand Down
26 changes: 26 additions & 0 deletions source/common/secret/secret_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "common/common/assert.h"
#include "common/secret/secret_provider_impl.h"
#include "common/ssl/certificate_validation_context_config_impl.h"
#include "common/ssl/tls_certificate_config_impl.h"

namespace Envoy {
Expand All @@ -21,6 +22,17 @@ void SecretManagerImpl::addStaticSecret(const envoy::api::v2::auth::Secret& secr
}
break;
}
case envoy::api::v2::auth::Secret::TypeCase::kValidationContext: {
auto secret_provider = std::make_shared<CertificateValidationContextConfigProviderImpl>(
secret.validation_context());
if (!static_certificate_validation_context_providers_
.insert(std::make_pair(secret.name(), secret_provider))
.second) {
throw EnvoyException(fmt::format(
"Duplicate static CertificateValidationContext secret name {}", secret.name()));
Copy link
Member

Choose a reason for hiding this comment

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

Need a unit test with EXPECT_THROW_WITH_MESSAGE for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tests TEST_F(SecretManagerImplTest, CertificateValidationContextSecretLoadSuccess) and TEST_F(SecretManagerImplTest, DuplicateStaticCertificateValidationContextSecret) are added into secret_manager_impl_test.cc

}
break;
}
default:
throw EnvoyException("Secret type not implemented");
}
Expand All @@ -32,10 +44,24 @@ SecretManagerImpl::findStaticTlsCertificateProvider(const std::string& name) con
return (secret != static_tls_certificate_providers_.end()) ? secret->second : nullptr;
}

CertificateValidationContextConfigProviderSharedPtr
SecretManagerImpl::findStaticCertificateValidationContextProvider(const std::string& name) const {
auto secret = static_certificate_validation_context_providers_.find(name);
Copy link
Member

Choose a reason for hiding this comment

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

Also needs unit test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tests TEST_F(SecretManagerImplTest, CertificateValidationContextSecretLoadSuccess) and TEST_F(SecretManagerImplTest, DuplicateStaticCertificateValidationContextSecret) are added into secret_manager_impl_test.cc

return (secret != static_certificate_validation_context_providers_.end()) ? secret->second
: nullptr;
}

TlsCertificateConfigProviderSharedPtr SecretManagerImpl::createInlineTlsCertificateProvider(
const envoy::api::v2::auth::TlsCertificate& tls_certificate) {
return std::make_shared<TlsCertificateConfigProviderImpl>(tls_certificate);
}

CertificateValidationContextConfigProviderSharedPtr
SecretManagerImpl::createInlineCertificateValidationContextProvider(
const envoy::api::v2::auth::CertificateValidationContext& certificate_validation_context) {
return std::make_shared<CertificateValidationContextConfigProviderImpl>(
certificate_validation_context);
}

} // namespace Secret
} // namespace Envoy
18 changes: 17 additions & 1 deletion source/common/secret/secret_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,40 @@

#include "envoy/secret/secret_manager.h"
#include "envoy/secret/secret_provider.h"
#include "envoy/ssl/certificate_validation_context_config.h"
#include "envoy/ssl/tls_certificate_config.h"

#include "common/common/logger.h"

namespace Envoy {
namespace Secret {

class SecretManagerImpl : public SecretManager, Logger::Loggable<Logger::Id::upstream> {
class SecretManagerImpl : public SecretManager, Logger::Loggable<Logger::Id::secret> {
public:
void addStaticSecret(const envoy::api::v2::auth::Secret& secret) override;

TlsCertificateConfigProviderSharedPtr
findStaticTlsCertificateProvider(const std::string& name) const override;

CertificateValidationContextConfigProviderSharedPtr
findStaticCertificateValidationContextProvider(const std::string& name) const override;

TlsCertificateConfigProviderSharedPtr createInlineTlsCertificateProvider(
const envoy::api::v2::auth::TlsCertificate& tls_certificate) override;

CertificateValidationContextConfigProviderSharedPtr
createInlineCertificateValidationContextProvider(
const envoy::api::v2::auth::CertificateValidationContext& certificate_validation_context)
override;

private:
// Manages pairs of secret name and TlsCertificateConfigProviderSharedPtr.
std::unordered_map<std::string, TlsCertificateConfigProviderSharedPtr>
static_tls_certificate_providers_;

// Manages pairs of secret name and CertificateValidationContextConfigProviderSharedPtr.
std::unordered_map<std::string, CertificateValidationContextConfigProviderSharedPtr>
static_certificate_validation_context_providers_;
};

} // namespace Secret
Expand Down
6 changes: 6 additions & 0 deletions source/common/secret/secret_provider_impl.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common/secret/secret_provider_impl.h"

#include "common/common/assert.h"
#include "common/ssl/certificate_validation_context_config_impl.h"
#include "common/ssl/tls_certificate_config_impl.h"

namespace Envoy {
Expand All @@ -10,5 +11,10 @@ TlsCertificateConfigProviderImpl::TlsCertificateConfigProviderImpl(
const envoy::api::v2::auth::TlsCertificate& tls_certificate)
: tls_certificate_(std::make_unique<Ssl::TlsCertificateConfigImpl>(tls_certificate)) {}

CertificateValidationContextConfigProviderImpl::CertificateValidationContextConfigProviderImpl(
const envoy::api::v2::auth::CertificateValidationContext& certificate_validation_context)
: certificate_validation_context_(std::make_unique<Ssl::CertificateValidationContextConfigImpl>(
certificate_validation_context)) {}

} // namespace Secret
} // namespace Envoy
17 changes: 17 additions & 0 deletions source/common/secret/secret_provider_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "envoy/api/v2/auth/cert.pb.h"
#include "envoy/secret/secret_provider.h"
#include "envoy/ssl/certificate_validation_context_config.h"
#include "envoy/ssl/tls_certificate_config.h"

namespace Envoy {
Expand All @@ -21,5 +22,21 @@ class TlsCertificateConfigProviderImpl : public TlsCertificateConfigProvider {
Ssl::TlsCertificateConfigPtr tls_certificate_;
};

class CertificateValidationContextConfigProviderImpl
: public CertificateValidationContextConfigProvider {
public:
CertificateValidationContextConfigProviderImpl(
const envoy::api::v2::auth::CertificateValidationContext& certificate_validation_context);

const Ssl::CertificateValidationContextConfig* secret() const override {
return certificate_validation_context_.get();
}

Common::CallbackHandle* addUpdateCallback(std::function<void()>) override { return nullptr; }

private:
Ssl::CertificateValidationContextConfigPtr certificate_validation_context_;
};

} // namespace Secret
} // namespace Envoy
12 changes: 12 additions & 0 deletions source/common/ssl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "certificate_validation_context_config_impl_lib",
srcs = ["certificate_validation_context_config_impl.cc"],
hdrs = ["certificate_validation_context_config_impl.h"],
deps = [
"//include/envoy/ssl:certificate_validation_context_config_interface",
"//source/common/common:empty_string",
"//source/common/config:datasource_lib",
"@envoy_api//envoy/api/v2/auth:cert_cc",
],
)

envoy_cc_library(
name = "utility_lib",
srcs = ["utility.cc"],
Expand Down
Loading