-
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.
Signed-off-by: Appu Goundan <[email protected]>
- Loading branch information
1 parent
70ebdbe
commit be5deff
Showing
16 changed files
with
545 additions
and
481 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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
/* | ||
* 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; | ||
|
||
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> findMeta( | ||
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 setRoot(Root root) throws IOException { | ||
// call storeRoot instead of generic storeMeta because it does extra work when storing on disk | ||
localStore.setRoot(root); | ||
cache.put(RootRole.ROOT, root); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends SignedTufMeta<? extends TufMeta>> Optional<T> findMeta( | ||
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.findMeta(roleName, tClass); | ||
value.ifPresent(v -> cache.put(roleName, v)); | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public void setMeta(String roleName, SignedTufMeta<? extends TufMeta> meta) throws IOException { | ||
if (Objects.equals(roleName, RootRole.ROOT)) { | ||
throw new IllegalArgumentException("Calling setMeta on root instead of setRoot"); | ||
} | ||
localStore.setMeta(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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
/* | ||
* 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; | ||
|
||
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; | ||
} |
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
/* | ||
* 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 defined a mutable meta store functionality. */ | ||
public interface TargetStore extends TargetReader { | ||
|
||
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.