-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #836 from sigstore/store-state-in-updater
Store meta state in memory
- Loading branch information
Showing
14 changed files
with
690 additions
and
380 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
37 changes: 37 additions & 0 deletions
37
sigstore-java/src/main/java/dev/sigstore/tuf/MetaReader.java
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,37 @@ | ||
/* | ||
* Copyright 2024 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf; | ||
|
||
import dev.sigstore.tuf.model.SignedTufMeta; | ||
import dev.sigstore.tuf.model.TufMeta; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
/** Interface that defines reading meta from local storage. */ | ||
public interface MetaReader { | ||
|
||
/** | ||
* Return a named metadata item if there is any. | ||
* | ||
* @param roleName the name of the role to load (root, timestamp, snapshot, targets, or a | ||
* delegated target role) | ||
* @param tClass the class type | ||
* @return an instance of the signed metadata for the role if it was found | ||
* @throws IOException if an error occurs reading from the backing store | ||
*/ | ||
<T extends SignedTufMeta<? extends TufMeta>> Optional<T> readMeta( | ||
String roleName, Class<T> tClass) throws IOException; | ||
} |
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
85 changes: 85 additions & 0 deletions
85
sigstore-java/src/main/java/dev/sigstore/tuf/PassthroughCacheMetaStore.java
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,85 @@ | ||
/* | ||
* Copyright 2024 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf; | ||
|
||
import dev.sigstore.tuf.model.Root; | ||
import dev.sigstore.tuf.model.RootRole; | ||
import dev.sigstore.tuf.model.SignedTufMeta; | ||
import dev.sigstore.tuf.model.TufMeta; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** An in memory cache that will pass through to a provided local tuf store. */ | ||
public class PassthroughCacheMetaStore implements MetaReader, MetaStore { | ||
private final MetaStore localStore; | ||
private final Map<String, SignedTufMeta<? extends TufMeta>> cache; | ||
|
||
private PassthroughCacheMetaStore(MetaStore localStore) { | ||
this.localStore = localStore; | ||
this.cache = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public String getIdentifier() { | ||
return "In memory cache backed by: " + localStore.getIdentifier(); | ||
} | ||
|
||
public static PassthroughCacheMetaStore newPassthroughMetaCache(MetaStore localStore) { | ||
return new PassthroughCacheMetaStore(localStore); | ||
} | ||
|
||
@Override | ||
public void writeRoot(Root root) throws IOException { | ||
// call writeRoot instead of generic writeMeta because it may do extra work when storing on disk | ||
localStore.writeRoot(root); | ||
cache.put(RootRole.ROOT, root); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends SignedTufMeta<? extends TufMeta>> Optional<T> readMeta( | ||
String roleName, Class<T> tClass) throws IOException { | ||
// check memory cache | ||
if (cache.containsKey(roleName)) { | ||
return Optional.of((T) cache.get(roleName)); | ||
} | ||
|
||
// check backing storage and write to memory if found | ||
var value = localStore.readMeta(roleName, tClass); | ||
value.ifPresent(v -> cache.put(roleName, v)); | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public void writeMeta(String roleName, SignedTufMeta<? extends TufMeta> meta) throws IOException { | ||
if (Objects.equals(roleName, RootRole.ROOT)) { | ||
throw new IllegalArgumentException("Calling writeMeta on root instead of writeRoot"); | ||
} | ||
localStore.writeMeta(roleName, meta); | ||
cache.put(roleName, meta); | ||
} | ||
|
||
@Override | ||
public void clearMetaDueToKeyRotation() throws IOException { | ||
localStore.clearMetaDueToKeyRotation(); | ||
cache.remove(RootRole.TIMESTAMP); | ||
cache.remove(RootRole.SNAPSHOT); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
sigstore-java/src/main/java/dev/sigstore/tuf/TargetReader.java
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,31 @@ | ||
/* | ||
* Copyright 2024 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf; | ||
|
||
import java.io.IOException; | ||
|
||
/** Interface that defines reading targets from local storage. */ | ||
public interface TargetReader { | ||
|
||
/** | ||
* Reads a TUF target file from the local TUF store | ||
* | ||
* @param targetName the name of the target file to read (e.g. ctfe.pub) | ||
* @return the content of the file as bytes | ||
* @throws IOException if an error occurs | ||
*/ | ||
byte[] readTarget(String targetName) throws IOException; | ||
} |
38 changes: 38 additions & 0 deletions
38
sigstore-java/src/main/java/dev/sigstore/tuf/TargetStore.java
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,38 @@ | ||
/* | ||
* Copyright 2024 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf; | ||
|
||
import java.io.IOException; | ||
|
||
/** Interface that defines a mutable target store functionality. */ | ||
public interface TargetStore extends TargetReader { | ||
|
||
/** | ||
* A generic string for identifying the local store in debug messages. A file system based | ||
* implementation might return the path being used for storage, while an in-memory store may just | ||
* return something like 'in-memory'. | ||
*/ | ||
String getIdentifier(); | ||
|
||
/** | ||
* Writes a TUF target to the local target store. | ||
* | ||
* @param targetName the name of the target file to write (e.g. ctfe.pub) | ||
* @param targetContents the content of the target file as bytes | ||
* @throws IOException if an error occurs | ||
*/ | ||
void writeTarget(String targetName, byte[] targetContents) throws IOException; | ||
} |
Oops, something went wrong.