-
Notifications
You must be signed in to change notification settings - Fork 801
feat: add plugin lockfile integrity (plugins.lock) #7394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pditommaso
wants to merge
4
commits into
master
Choose a base branch
from
plugin-lockfile-integrity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51a8232
feat: add plugin lockfile integrity (plugins.lock)
pditommaso 8255ec7
refactor: pin extracted-tree hash instead of archive hash
pditommaso 03549d9
Merge branch 'master' into plugin-lockfile-integrity
pditommaso 1932241
Merge branch 'master' into plugin-lockfile-integrity
pditommaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
177 changes: 177 additions & 0 deletions
177
modules/nf-commons/src/main/nextflow/plugin/PluginLockFile.groovy
This file contains hidden or 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,177 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * 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 nextflow.plugin | ||
|
|
||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
|
|
||
| import com.google.gson.Gson | ||
| import com.google.gson.GsonBuilder | ||
| import com.google.gson.JsonSyntaxException | ||
| import groovy.transform.CompileStatic | ||
| import groovy.transform.EqualsAndHashCode | ||
| import groovy.transform.ToString | ||
| import groovy.util.logging.Slf4j | ||
|
|
||
| /** | ||
| * Model the {@code plugins.lock} file. It holds a format version number and a map | ||
| * of plugin fully-qualified ids (ie. {@code id@version}) to the corresponding | ||
| * {@link Entry} carrying the {@code sha512} checksum of the plugin archive. | ||
| * | ||
| * The file is serialised as pretty-printed JSON with a stable (sorted) key order to | ||
| * keep diffs deterministic. | ||
| * | ||
| * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
| */ | ||
| @Slf4j | ||
| @CompileStatic | ||
| @EqualsAndHashCode(includeFields = true) | ||
| @ToString(includeNames = true, includeFields = true) | ||
| class PluginLockFile { | ||
|
|
||
| /** | ||
| * The current lock file format version | ||
| */ | ||
| static final int CURRENT_VERSION = 1 | ||
|
|
||
| /** | ||
| * Represent a single locked plugin entry | ||
| */ | ||
| @EqualsAndHashCode | ||
| @ToString(includeNames = true) | ||
| static class Entry { | ||
| String sha512 | ||
|
|
||
| Entry() {} | ||
|
|
||
| Entry(String sha512) { | ||
| this.sha512 = sha512 | ||
| } | ||
| } | ||
|
|
||
| private int version = CURRENT_VERSION | ||
|
|
||
| private Map<String,Entry> plugins = new TreeMap<String,Entry>() | ||
|
|
||
| int getVersion() { version } | ||
|
|
||
| void setVersion(int value) { this.version = value } | ||
|
|
||
| /** | ||
| * @return An immutable view of the locked plugin entries, keyed by fully-qualified id | ||
| */ | ||
| Map<String,Entry> getEntries() { | ||
| return Collections.unmodifiableMap(plugins) | ||
| } | ||
|
|
||
| /** | ||
| * Lookup a locked entry by its fully-qualified id ie. {@code id@version}. | ||
| * | ||
| * @param fqid The plugin fully-qualified id | ||
| * @return The corresponding {@link Entry} or {@code null} if not present | ||
| */ | ||
| Entry getEntry(String fqid) { | ||
| return plugins.get(fqid) | ||
| } | ||
|
|
||
| /** | ||
| * Add or update a locked entry. | ||
| * | ||
| * @param fqid The plugin fully-qualified id ie. {@code id@version} | ||
| * @param entry The {@link Entry} to associate with the given id | ||
| * @return The object itself to enable method chaining | ||
| */ | ||
| PluginLockFile addEntry(String fqid, Entry entry) { | ||
| if( !fqid ) | ||
| throw new IllegalArgumentException("Plugin lock entry id cannot be empty") | ||
| if( entry == null ) | ||
| throw new IllegalArgumentException("Plugin lock entry cannot be null") | ||
| plugins.put(fqid, entry) | ||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * @return {@code true} when no plugin entries are held | ||
| */ | ||
| boolean isEmpty() { | ||
| return plugins.isEmpty() | ||
| } | ||
|
|
||
| /** | ||
| * Serialise this lock file as pretty-printed JSON with a stable key order. | ||
| * | ||
| * @param path The target file path | ||
| */ | ||
| void write(Path path) { | ||
| final json = gson0().toJson(toModel()) | ||
| Files.write(path, json.getBytes('UTF-8')) | ||
| } | ||
|
|
||
| private Map<String,Object> toModel() { | ||
| // use a plain map so that only 'version' and 'plugins' are emitted, with | ||
| // plugins held in a TreeMap to guarantee a stable, sorted key order | ||
| final result = new LinkedHashMap<String,Object>() | ||
| result.put('version', version) | ||
| result.put('plugins', new TreeMap<String,Entry>(plugins)) | ||
| return result | ||
| } | ||
|
|
||
| private static Gson gson0() { | ||
| return new GsonBuilder().setPrettyPrinting().create() | ||
| } | ||
|
|
||
| /** | ||
| * Read and parse a {@code plugins.lock} file. | ||
| * | ||
| * @param path The lock file path | ||
| * @return A {@link PluginLockFile}; an empty (dormant) instance when the file does not exist | ||
| * @throws IllegalStateException when the file content cannot be parsed | ||
| */ | ||
| static PluginLockFile read(Path path) { | ||
| if( path == null || !Files.exists(path) ) { | ||
| log.debug "Plugins lock file does not exist: $path - returning an empty lock" | ||
| return new PluginLockFile() | ||
| } | ||
|
|
||
| final text = new String(Files.readAllBytes(path), 'UTF-8') | ||
| // a blank or freshly `touch`ed file is a valid, empty lock (bootstrap case) | ||
| if( !text.trim() ) | ||
| return new PluginLockFile() | ||
| try { | ||
| final model = gson0().fromJson(text, ModelBean) | ||
| if( model == null ) | ||
| throw new IllegalStateException("Invalid plugins lock file - empty content: $path") | ||
| final result = new PluginLockFile() | ||
| result.version = model.version | ||
| if( model.plugins ) | ||
| result.plugins.putAll(model.plugins) | ||
| return result | ||
| } | ||
| catch( JsonSyntaxException e ) { | ||
| throw new IllegalStateException("Invalid plugins lock file - malformed JSON: $path", e) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deserialization bean matching the on-disk JSON structure | ||
| */ | ||
| static class ModelBean { | ||
| int version | ||
| Map<String,Entry> plugins | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.