|
| 1 | +// Copyright 2015 Matthew Holt |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package certmagic |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + "path" |
| 21 | + "strings" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "golang.org/x/sync/semaphore" |
| 26 | +) |
| 27 | + |
| 28 | +type storageEntry struct { |
| 29 | + i KeyInfo |
| 30 | + d []byte |
| 31 | +} |
| 32 | + |
| 33 | +// memoryStorage is a Storage implemention that exists only in memory |
| 34 | +// it is intended for testing and one-time-deploys where no persistence is needed |
| 35 | +type memoryStorage struct { |
| 36 | + m map[string]storageEntry |
| 37 | + mu sync.RWMutex |
| 38 | + |
| 39 | + kmu *keyMutex |
| 40 | +} |
| 41 | + |
| 42 | +func NewMemoryStorage() Storage { |
| 43 | + return &memoryStorage{ |
| 44 | + m: map[string]storageEntry{}, |
| 45 | + kmu: newKeyMutex(), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Exists returns true if key exists in s. |
| 50 | +func (s *memoryStorage) Exists(ctx context.Context, key string) bool { |
| 51 | + ans, err := s.List(ctx, key, true) |
| 52 | + if err != nil { |
| 53 | + return false |
| 54 | + } |
| 55 | + return len(ans) != 0 |
| 56 | +} |
| 57 | + |
| 58 | +// Store saves value at key. |
| 59 | +func (s *memoryStorage) Store(_ context.Context, key string, value []byte) error { |
| 60 | + s.mu.Lock() |
| 61 | + defer s.mu.Unlock() |
| 62 | + s.m[key] = storageEntry{ |
| 63 | + i: KeyInfo{ |
| 64 | + Key: key, |
| 65 | + Modified: time.Now(), |
| 66 | + Size: int64(len(value)), |
| 67 | + IsTerminal: true, |
| 68 | + }, |
| 69 | + d: value, |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// Load retrieves the value at key. |
| 75 | +func (s *memoryStorage) Load(_ context.Context, key string) ([]byte, error) { |
| 76 | + s.mu.Lock() |
| 77 | + defer s.mu.Unlock() |
| 78 | + val, ok := s.m[key] |
| 79 | + if !ok { |
| 80 | + return nil, os.ErrNotExist |
| 81 | + } |
| 82 | + return val.d, nil |
| 83 | +} |
| 84 | + |
| 85 | +// Delete deletes the value at key. |
| 86 | +func (s *memoryStorage) Delete(_ context.Context, key string) error { |
| 87 | + s.mu.Lock() |
| 88 | + defer s.mu.Unlock() |
| 89 | + delete(s.m, key) |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// List returns all keys that match prefix. |
| 94 | +func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) { |
| 95 | + s.mu.Lock() |
| 96 | + defer s.mu.Unlock() |
| 97 | + return s.list(ctx, prefix, recursive) |
| 98 | +} |
| 99 | + |
| 100 | +func (s *memoryStorage) list(ctx context.Context, prefix string, recursive bool) ([]string, error) { |
| 101 | + var keyList []string |
| 102 | + |
| 103 | + keys := make([]string, 0, len(s.m)) |
| 104 | + for k := range s.m { |
| 105 | + if strings.HasPrefix(k, prefix) { |
| 106 | + keys = append(keys, k) |
| 107 | + } |
| 108 | + } |
| 109 | + // adapted from https://github.com/pberkel/caddy-storage-redis/blob/main/storage.go#L369 |
| 110 | + // Iterate over each child key |
| 111 | + for _, k := range keys { |
| 112 | + // Directory keys will have a "/" suffix |
| 113 | + trimmedKey := strings.TrimSuffix(k, "/") |
| 114 | + // Reconstruct the full path of child key |
| 115 | + fullPathKey := path.Join(prefix, trimmedKey) |
| 116 | + // If current key is a directory |
| 117 | + if recursive && k != trimmedKey { |
| 118 | + // Recursively traverse all child directories |
| 119 | + childKeys, err := s.list(ctx, fullPathKey, recursive) |
| 120 | + if err != nil { |
| 121 | + return keyList, err |
| 122 | + } |
| 123 | + keyList = append(keyList, childKeys...) |
| 124 | + } else { |
| 125 | + keyList = append(keyList, fullPathKey) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return keys, nil |
| 130 | +} |
| 131 | + |
| 132 | +// Stat returns information about key. |
| 133 | +func (s *memoryStorage) Stat(_ context.Context, key string) (KeyInfo, error) { |
| 134 | + s.mu.Lock() |
| 135 | + defer s.mu.Unlock() |
| 136 | + val, ok := s.m[key] |
| 137 | + if !ok { |
| 138 | + return KeyInfo{}, os.ErrNotExist |
| 139 | + } |
| 140 | + return val.i, nil |
| 141 | +} |
| 142 | + |
| 143 | +// Lock obtains a lock named by the given name. It blocks |
| 144 | +// until the lock can be obtained or an error is returned. |
| 145 | +func (s *memoryStorage) Lock(ctx context.Context, name string) error { |
| 146 | + return s.kmu.LockKey(ctx, name) |
| 147 | +} |
| 148 | + |
| 149 | +// Unlock releases the lock for name. |
| 150 | +func (s *memoryStorage) Unlock(_ context.Context, name string) error { |
| 151 | + return s.kmu.UnlockKey(name) |
| 152 | +} |
| 153 | + |
| 154 | +func (s *memoryStorage) String() string { |
| 155 | + return "memoryStorage" |
| 156 | +} |
| 157 | + |
| 158 | +// Interface guard |
| 159 | +var _ Storage = (*memoryStorage)(nil) |
| 160 | + |
| 161 | +type keyMutex struct { |
| 162 | + m map[string]*semaphore.Weighted |
| 163 | + mu sync.Mutex |
| 164 | +} |
| 165 | + |
| 166 | +func newKeyMutex() *keyMutex { |
| 167 | + return &keyMutex{ |
| 168 | + m: map[string]*semaphore.Weighted{}, |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func (km *keyMutex) LockKey(ctx context.Context, id string) error { |
| 173 | + select { |
| 174 | + case <-ctx.Done(): |
| 175 | + // as a special case, caddy allows for the cancelled context to be used for a trylock. |
| 176 | + if km.mutex(id).TryAcquire(1) { |
| 177 | + return nil |
| 178 | + } |
| 179 | + return ctx.Err() |
| 180 | + default: |
| 181 | + return km.mutex(id).Acquire(ctx, 1) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// Releases the lock associated with the specified ID. |
| 186 | +func (km *keyMutex) UnlockKey(id string) error { |
| 187 | + km.mutex(id).Release(1) |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func (km *keyMutex) mutex(id string) *semaphore.Weighted { |
| 192 | + km.mu.Lock() |
| 193 | + defer km.mu.Unlock() |
| 194 | + val, ok := km.m[id] |
| 195 | + if !ok { |
| 196 | + val = semaphore.NewWeighted(1) |
| 197 | + km.m[id] = val |
| 198 | + } |
| 199 | + return val |
| 200 | +} |
0 commit comments