Skip to content

Commit

Permalink
Update authn filter to use first spiffe san (envoyproxy#2617)
Browse files Browse the repository at this point in the history
Signed-off-by: Prabu Shyam <prabushyam41@gmail.com>

Co-authored-by: Prabu Shyam <prabushyam@users.noreply.github.com>
  • Loading branch information
2 people authored and istio-testing committed Jan 3, 2020
1 parent 116466b commit 96a5090
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/envoy/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const std::string kPerHostMetadataKey("istio");
// Attribute field for per-host data override
const std::string kMetadataDestinationUID("uid");

bool hasSPIFFEPrefix(const std::string& san) {
return absl::StartsWith(san, kSPIFFEPrefix);
}

bool getCertSAN(const Network::Connection* connection, bool peer,
std::string* principal) {
if (connection) {
Expand All @@ -47,16 +51,21 @@ bool getCertSAN(const Network::Connection* connection, bool peer,
// empty result is not allowed.
return false;
}
// return the first san with the 'spiffe://' prefix
for (const auto& san : sans) {
if (hasSPIFFEPrefix(san)) {
*principal = san;
return true;
}
}
// return the first san if no sans have the spiffe:// prefix
*principal = sans[0];
return true;
}
}
return false;
}

bool hasSPIFFEPrefix(const std::string& san) {
return absl::StartsWith(san, kSPIFFEPrefix);
}
} // namespace

void ExtractHeaders(const Http::HeaderMap& header_map,
Expand Down
20 changes: 20 additions & 0 deletions src/envoy/utils/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ TEST_P(UtilsTest, GetPrincipalNoSpiffePrefix) {
testGetPrincipal(sans, "spiffe:foo/bar", true);
}

TEST_P(UtilsTest, GetPrincipalFromSpiffePrefixSAN) {
std::vector<std::string> sans{"bad", "spiffe://foo/bar"};
testGetPrincipal(sans, "foo/bar", true);
}

TEST_P(UtilsTest, GetPrincipalFromNonSpiffePrefixSAN) {
std::vector<std::string> sans{"foobar", "xyz"};
testGetPrincipal(sans, "foobar", true);
}

TEST_P(UtilsTest, GetPrincipalEmpty) {
std::vector<std::string> sans;
testGetPrincipal(sans, "", false);
Expand All @@ -148,6 +158,16 @@ TEST_P(UtilsTest, GetTrustDomainNoSpiffePrefix) {
testGetTrustDomain(sans, "", false);
}

TEST_P(UtilsTest, GetTrustDomainFromSpiffePrefixSAN) {
std::vector<std::string> sans{"bad", "spiffe://td/bar", "xyz"};
testGetTrustDomain(sans, "td", true);
}

TEST_P(UtilsTest, GetTrustDomainFromNonSpiffePrefixSAN) {
std::vector<std::string> sans{"tdbar", "xyz"};
testGetTrustDomain(sans, "", false);
}

TEST_P(UtilsTest, GetTrustDomainNoSlash) {
std::vector<std::string> sans{"spiffe://td", "bad"};
testGetTrustDomain(sans, "", false);
Expand Down

0 comments on commit 96a5090

Please sign in to comment.