| 1 | // Copyright © SAS Institute Inc. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package timestampcache |
| 15 | |
| 16 | import ( |
| 17 | "context" |
| 18 | "crypto/sha256" |
| 19 | "fmt" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/bradfitz/gomemcache/memcache" |
| 23 | "github.com/prometheus/client_golang/prometheus" |
| 24 | "github.com/prometheus/client_golang/prometheus/promauto" |
| 25 | "github.com/rs/zerolog/log" |
| 26 | |
| 27 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 28 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | memcacheTimeout = 1 * time.Second |
| 33 | memcacheExpiry = 7 * 24 * time.Hour |
| 34 | ) |
| 35 | |
| 36 | var metricHits = promauto.NewCounterVec( |
| 37 | prometheus.CounterOpts{ |
| 38 | Name: "timestamper_cache", |
| 39 | Help: "Timestamper cache hit and miss count", |
| 40 | }, |
| 41 | []string{"result"}, |
| 42 | ) |
| 43 | |
| 44 | type timestampCache struct { |
| 45 | Timestamper pkcs9.Timestamper |
| 46 | Memcache *memcache.Client |
| 47 | } |
| 48 | |
| 49 | func New(t pkcs9.Timestamper, servers []string) (pkcs9.Timestamper, error) { |
| 50 | selector := new(memcache.ServerList) |
| 51 | if err := selector.SetServers(servers...); err != nil { |
| 52 | return nil, fmt.Errorf("parsing memcache servers: %w", err) |
| 53 | } |
| 54 | mc := memcache.NewFromSelector(selector) |
| 55 | mc.Timeout = memcacheTimeout |
| 56 | return ×tampCache{t, mc}, nil |
| 57 | } |
| 58 | |
| 59 | func (c *timestampCache) Timestamp(ctx context.Context, req *pkcs9.Request) (*pkcs7.ContentInfoSignedData, error) { |
| 60 | key := cacheKey(req) |
| 61 | item, err := c.Memcache.Get(key) |
| 62 | if err == nil { |
| 63 | token, err := pkcs7.Unmarshal(item.Value) |
| 64 | if err == nil { |
| 65 | metricHits.WithLabelValues("hit").Inc() |
| 66 | return token, nil |
| 67 | } |
| 68 | log.Warn().Err(err).Str("key", key).Msg("failed to parse cached value for timestamp") |
| 69 | // bad cached value, fall through |
| 70 | } |
| 71 | token, err := c.Timestamper.Timestamp(ctx, req) |
| 72 | if err == nil { |
| 73 | blob, err := token.Marshal() |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | if err := c.Memcache.Set(&memcache.Item{ |
| 78 | Key: key, |
| 79 | Value: blob, |
| 80 | Expiration: int32(memcacheExpiry / time.Second), |
| 81 | }); err != nil { |
| 82 | log.Warn().Err(err).Msg("failed to save cached value for timestamp") |
| 83 | } |
| 84 | metricHits.WithLabelValues("miss").Inc() |
| 85 | } |
| 86 | return token, err |
| 87 | } |
| 88 | |
| 89 | func cacheKey(req *pkcs9.Request) string { |
| 90 | d := sha256.New() |
| 91 | d.Write(req.EncryptedDigest) |
| 92 | prefix := "pkcs9" |
| 93 | if req.Legacy { |
| 94 | prefix = "msft" |
| 95 | } |
| 96 | return fmt.Sprintf("%s-%d-%x", prefix, req.Hash, d.Sum(nil)) |
| 97 | } |