-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into td-macos-travis
- Loading branch information
Showing
9 changed files
with
311 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bundleutil | ||
|
||
import ( | ||
"math" | ||
"time" | ||
) | ||
|
||
const ( | ||
refreshHintLeewayFactor = 10 | ||
|
||
// MinimumRefreshHint is the smallest refresh hint the client allows. | ||
// Anything smaller than the minimum will be reset to the minimum. | ||
MinimumRefreshHint = time.Minute | ||
) | ||
|
||
// CalculateRefreshHint is used to calculate the refresh hint for a given | ||
// bundle. If the bundle already contains a refresh hint, then that is used, | ||
// Otherwise, it looks at the lifetimes of the bundle contents and returns a | ||
// fraction of the smallest. It is fairly aggressive but ensures clients don't | ||
// miss a rotation period and lose their ability to fetch. | ||
// TODO: reevaluate our strategy here when we rework the TTL story inside SPIRE. | ||
func CalculateRefreshHint(bundle *Bundle) time.Duration { | ||
if r := bundle.RefreshHint(); r > 0 { | ||
return safeRefreshHint(r) | ||
} | ||
|
||
const maxDuration time.Duration = math.MaxInt64 | ||
|
||
smallestLifetime := maxDuration | ||
for _, rootCA := range bundle.RootCAs() { | ||
lifetime := rootCA.NotAfter.Sub(rootCA.NotBefore) | ||
if lifetime < smallestLifetime { | ||
smallestLifetime = lifetime | ||
} | ||
} | ||
|
||
// TODO: look at JWT key lifetimes... requires us to track issued_at dates | ||
// which we currently do not do. | ||
|
||
// Set the refresh hint to a fraction of the smallest lifetime, if found. | ||
var refreshHint time.Duration | ||
if smallestLifetime != maxDuration { | ||
refreshHint = smallestLifetime / refreshHintLeewayFactor | ||
} | ||
return safeRefreshHint(refreshHint) | ||
} | ||
|
||
func safeRefreshHint(refreshHint time.Duration) time.Duration { | ||
if refreshHint < MinimumRefreshHint { | ||
return MinimumRefreshHint | ||
} | ||
return refreshHint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package bundleutil | ||
|
||
import ( | ||
"crypto/x509" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spiffe/spire/proto/spire/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCalculateRefreshHint(t *testing.T) { | ||
emptyBundle := New("domain.test") | ||
|
||
emptyBundleWithRefreshHint, err := BundleFromProto(&common.Bundle{ | ||
TrustDomainId: "domain.test", | ||
RefreshHint: 3600, | ||
}) | ||
require.NoError(t, err) | ||
|
||
now := time.Now() | ||
bundleWithCerts := New("domain.test") | ||
bundleWithCerts.AppendRootCA(&x509.Certificate{ | ||
NotBefore: now, | ||
NotAfter: now.Add(time.Hour * 2), | ||
}) | ||
bundleWithCerts.AppendRootCA(&x509.Certificate{ | ||
NotBefore: now, | ||
NotAfter: now.Add(time.Hour), | ||
}) | ||
bundleWithCerts.AppendRootCA(&x509.Certificate{ | ||
NotBefore: now, | ||
NotAfter: now.Add(time.Hour * 3), | ||
}) | ||
|
||
testCases := []struct { | ||
name string | ||
bundle *Bundle | ||
refreshHint time.Duration | ||
}{ | ||
{ | ||
name: "empty bundle with no refresh hint", | ||
bundle: emptyBundle, | ||
refreshHint: MinimumRefreshHint, | ||
}, | ||
{ | ||
name: "empty bundle with refresh hint", | ||
bundle: emptyBundleWithRefreshHint, | ||
refreshHint: time.Hour, | ||
}, | ||
{ | ||
// the bundle has a few certs. the lowest lifetime is 1 hour. | ||
// so we expect to get back a fraction of that time. | ||
name: "bundle with certs", | ||
bundle: bundleWithCerts, | ||
refreshHint: time.Hour / refreshHintLeewayFactor, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
require.Equal(t, testCase.refreshHint, CalculateRefreshHint(testCase.bundle), "refresh hint is wrong") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.