Skip to content

Rewrite registry resource to support delete operation #379

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

Merged
merged 10 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"rust-analyzer.linkedProjects": [
"./dsc/Cargo.toml",
"./dsc_lib/Cargo.toml",
"./ntreg/Cargo.toml",
"./ntstatuserror/Cargo.toml",
"./ntuserinfo/Cargo.toml",
"./osinfo/Cargo.toml",
"./registry/Cargo.toml",
"./tools/test_group_resource/Cargo.toml",
Expand All @@ -22,5 +19,6 @@
"yaml.schemas": {
"schemas/2023/10/bundled/config/document.vscode.json": "**.dsc.{yaml,yml,config.yaml,config.yml}",
"schemas/2023/10/bundled/resource/manifest.vscode.json": "**.dsc.resource.{yaml,yml}"
}
},
"sarif-viewer.connectToGithubCodeScanning": "off"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions archive/registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "registry"
version = "0.1.0"
edition = "2021"

[profile.release]
strip = true
# optimize for size
opt-level = 2
# enable link time optimization to remove dead code
lto = true

[profile.dev]
lto = true

[dependencies]
atty = { version = "0.2" }
clap = { version = "4.1", features = ["derive"] }
crossterm = { version = "0.26" }
ntreg = { path = "../ntreg" }
ntstatuserror = { path = "../ntstatuserror" }
schemars = { version = "0.8" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }

[target.'cfg(onecore)'.dependencies]
pal = { path = "../pal" }

[build-dependencies]
static_vcruntime = "2.0"
File renamed without changes.
18 changes: 18 additions & 0 deletions archive/registry/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#[cfg(onecore)]
fn main() {
// Prevent this build script from rerunning unnecessarily.
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rustc-link-lib=onecore_apiset");
println!("cargo:rustc-link-lib=onecoreuap_apiset");
static_vcruntime::metabuild();
}

#[cfg(not(onecore))]
fn main() {
// Prevent this build script from rerunning unnecessarily.
println!("cargo:rerun-if-changed=build.rs");
static_vcruntime::metabuild();
}
52 changes: 52 additions & 0 deletions archive/registry/registry.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json",
"type": "Microsoft.Windows/Registry",
"description": "Manage Windows Registry keys and values",
"tags": [
"Windows",
"NT"
],
"version": "0.1.0",
"get": {
"executable": "registry",
"args": [
"config",
"get"
],
"input": "stdin"
},
"set": {
"executable": "registry",
"args": [
"config",
"set"
],
"input": "stdin",
"implementsPretest": true,
"return": "state"
},
"test": {
"executable": "registry",
"args": [
"config",
"test"
],
"input": "stdin",
"return": "state"
},
"exitCodes": {
"0": "Success",
"1": "Invalid parameter",
"2": "Invalid input",
"3": "Registry error",
"4": "JSON serialization failed"
},
"schema": {
"command": {
"executable": "registry",
"args": [
"schema"
]
}
}
}
76 changes: 76 additions & 0 deletions archive/registry/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[clap(name = "registry", version = "0.0.1", about = "Manage state of Windows registry", long_about = None)]
pub struct Arguments {

#[clap(subcommand)]
pub subcommand: SubCommand,
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
pub enum ConfigSubCommand {
#[clap(name = "get", about = "Retrieve registry configuration.")]
Get,
#[clap(name = "set", about = "Apply registry configuration.")]
Set,
#[clap(name = "test", about = "Validate registry configuration.")]
Test,
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
pub enum SubCommand {
#[clap(name = "query", about = "Query a registry key or value.", arg_required_else_help = true)]
Query {
#[clap(short, long, required = true, help = "The registry key path to query.")]
key_path: String,
#[clap(short, long, help = "The name of the value to query.")]
value_name: Option<String>,
#[clap(short, long, help = "Recursively query subkeys.")]
recurse: bool,
},
#[clap(name = "set", about = "Set a registry key or value.")]
Set {
#[clap(short, long, required = true, help = "The registry key path to set.")]
key_path: String,
#[clap(short, long, help = "The value to set.")]
value: String,
},
#[clap(name = "test", about = "Validate registry matches input JSON.")]
Test,
#[clap(name = "remove", about = "Remove a registry key or value.", arg_required_else_help = true)]
Remove {
#[clap(short, long, required = true, help = "The registry key path to remove.")]
key_path: String,
#[clap(short, long, help = "The name of the value to remove.")]
value_name: Option<String>,
#[clap(short, long, help = "Recursively remove subkeys.")]
recurse: bool,
},
#[clap(name = "find", about = "Find a registry key or value.", arg_required_else_help = true)]
Find {
#[clap(short, long, required = true, help = "The registry key path to start find.")]
key_path: String,
#[clap(short, long, required = true, help = "The string to find.")]
find: String,
#[clap(short, long, help = "Recursively find.")]
recurse: bool,
#[clap(long, help = "Only find keys.")]
keys_only: bool,
#[clap(long, help = "Only find values.")]
values_only: bool,
},
#[clap(name = "config", about = "Manage registry configuration.", arg_required_else_help = true)]
Config {
#[clap(subcommand)]
subcommand: ConfigSubCommand,
},
#[clap(name = "schema", about = "Retrieve JSON schema.")]
Schema {
#[clap(short, long, help = "Pretty print JSON.")]
pretty: bool,
}
}
File renamed without changes.
74 changes: 74 additions & 0 deletions archive/registry/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum RegistryValueData {
String(String),
ExpandString(String),
Binary(Vec<u8>),
DWord(u32),
MultiString(Vec<String>),
QWord(u64),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename = "Registry", deny_unknown_fields)]
pub struct Registry {
/// The ID of the resource. Value is ignored for input.
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
/// The path to the registry key.
#[serde(rename = "keyPath")]
pub key_path: String,
/// The name of the registry value.
#[serde(rename = "valueName")]
#[serde(skip_serializing_if = "Option::is_none")]
pub value_name: Option<String>,
/// The data of the registry value.
#[serde(rename = "valueData")]
#[serde(skip_serializing_if = "Option::is_none")]
pub value_data: Option<RegistryValueData>,
/// Flag indicating whether the registry value should be present or absent.
#[serde(rename = "_exist")]
#[serde(skip_serializing_if = "Option::is_none")]
pub exist: Option<bool>,
/// Flag indicating whether the registry value should be overwritten if it already exists.
#[serde(rename = "_clobber")]
#[serde(skip_serializing_if = "Option::is_none")]
pub clobber: Option<bool>,
/// Flag indicating whether the resource is in the desired state. Value is ignored for input.
#[serde(rename = "_inDesiredState")]
#[serde(skip_serializing_if = "Option::is_none")]
pub in_desired_state: Option<bool>,
}

impl Registry {
pub fn to_json(&self) -> String {
match serde_json::to_string(self) {
Ok(json) => json,
Err(e) => {
eprintln!("Failed to serialize to JSON: {e}");
String::new()
}
}
}
}

const ID: &str = "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json";

impl Default for Registry {
fn default() -> Self {
Self {
id: Some(ID.to_string()),
key_path: String::new(),
value_name: None,
value_data: None,
exist: None,
clobber: None,
in_desired_state: None,
}
}
}
Loading