Debian Codegraph @

lib/src/secret_backend.rs

raw
1 // Copyright 2024 The Jujutsu Authors
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 // https://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 //! Provides a backend for testing ACLs
16
17 use std::path::Path;
18 use std::pin::Pin;
19 use std::time::SystemTime;
20
21 use async_trait::async_trait;
22 use futures::AsyncRead;
23 use futures::stream::BoxStream;
24
25 use crate::backend::Backend;
26 use crate::backend::BackendError;
27 use crate::backend::BackendLoadError;
28 use crate::backend::BackendResult;
29 use crate::backend::ChangeId;
30 use crate::backend::Commit;
31 use crate::backend::CommitId;
32 use crate::backend::CopyHistory;
33 use crate::backend::CopyId;
34 use crate::backend::CopyRecord;
35 use crate::backend::FileId;
36 use crate::backend::RelatedCopy;
37 use crate::backend::SigningFn;
38 use crate::backend::SymlinkId;
39 use crate::backend::Tree;
40 use crate::backend::TreeId;
41 use crate::git_backend::GitBackend;
42 use crate::index::Index;
43 use crate::object_id::ObjectId as _;
44 use crate::repo_path::RepoPath;
45 use crate::repo_path::RepoPathBuf;
46 use crate::settings::UserSettings;
47
48 const SECRET_CONTENTS_HEX: [&str; 2] = [
49 "d97c5eada5d8c52079031eef0107a4430a9617c5", // "secret\n"
50 "536aca34dbae6b2b8af26bebdcba83543c9546f0", // "secret"
51 ];
52
53 /// A commit backend that's completely compatible with the Git backend, except
54 /// that it refuses to read files and symlinks with the word "secret" in the
55 /// path, or "secret" or "secret\n" in the content.
56 #[derive(Debug)]
57 pub struct SecretBackend {
58 inner: GitBackend,
59 }
60
61 impl SecretBackend {
62 /// "secret"
63 pub fn name() -> &'static str {
64 "secret"
65 }
66
67 /// Loads the backend from the given path.
68 pub fn load(settings: &UserSettings, store_path: &Path) -> Result<Self, BackendLoadError> {
69 let inner = GitBackend::load(settings, store_path)?;
70 Ok(Self { inner })
71 }
72
73 /// Convert a git repo to using `SecretBackend`
74 // TODO: Avoid this hack
75 pub fn adopt_git_repo(workspace_path: &Path) {
76 std::fs::write(
77 workspace_path
78 .join(".jj")
79 .join("repo")
80 .join("store")
81 .join("type"),
82 Self::name(),
83 )
84 .unwrap();
85 }
86 }
87
88 #[async_trait]
89 impl Backend for SecretBackend {
90 fn name(&self) -> &str {
91 Self::name()
92 }
93
94 fn commit_id_length(&self) -> usize {
95 self.inner.commit_id_length()
96 }
97
98 fn change_id_length(&self) -> usize {
99 self.inner.change_id_length()
100 }
101
102 fn root_commit_id(&self) -> &CommitId {
103 self.inner.root_commit_id()
104 }
105
106 fn root_change_id(&self) -> &ChangeId {
107 self.inner.root_change_id()
108 }
109
110 fn empty_tree_id(&self) -> &TreeId {
111 self.inner.empty_tree_id()
112 }
113
114 fn concurrency(&self) -> usize {
115 1
116 }
117
118 async fn read_file(
119 &self,
120 path: &RepoPath,
121 id: &FileId,
122 ) -> BackendResult<Pin<Box<dyn AsyncRead + Send>>> {
123 if path.as_internal_file_string().contains("secret")
124 || SECRET_CONTENTS_HEX.contains(&id.hex().as_ref())
125 {
126 return Err(BackendError::ReadAccessDenied {
127 object_type: "file".to_string(),
128 hash: id.hex(),
129 source: "No access".into(),
130 });
131 }
132 self.inner.read_file(path, id).await
133 }
134
135 async fn write_file(
136 &self,
137 path: &RepoPath,
138 contents: &mut (dyn AsyncRead + Send + Unpin),
139 ) -> BackendResult<FileId> {
140 self.inner.write_file(path, contents).await
141 }
142
143 async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
144 if path.as_internal_file_string().contains("secret")
145 || SECRET_CONTENTS_HEX.contains(&id.hex().as_ref())
146 {
147 return Err(BackendError::ReadAccessDenied {
148 object_type: "symlink".to_string(),
149 hash: id.hex(),
150 source: "No access".into(),
151 });
152 }
153 self.inner.read_symlink(path, id).await
154 }
155
156 async fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId> {
157 self.inner.write_symlink(path, target).await
158 }
159
160 async fn read_copy(&self, _id: &CopyId) -> BackendResult<CopyHistory> {
161 Err(BackendError::Unsupported(
162 "The secret backend doesn't support copies".to_string(),
163 ))
164 }
165
166 async fn write_copy(&self, _contents: &CopyHistory) -> BackendResult<CopyId> {
167 Err(BackendError::Unsupported(
168 "The secret backend doesn't support copies".to_string(),
169 ))
170 }
171
172 async fn get_related_copies(&self, _copy_id: &CopyId) -> BackendResult<Vec<RelatedCopy>> {
173 Err(BackendError::Unsupported(
174 "The secret backend doesn't support copies".to_string(),
175 ))
176 }
177
178 async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
179 self.inner.read_tree(path, id).await
180 }
181
182 async fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId> {
183 self.inner.write_tree(path, contents).await
184 }
185
186 async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
187 self.inner.read_commit(id).await
188 }
189
190 async fn write_commit(
191 &self,
192 contents: Commit,
193 sign_with: Option<&mut SigningFn>,
194 ) -> BackendResult<(CommitId, Commit)> {
195 self.inner.write_commit(contents, sign_with).await
196 }
197
198 fn get_copy_records(
199 &self,
200 paths: Option<&[RepoPathBuf]>,
201 root: &CommitId,
202 head: &CommitId,
203 ) -> BackendResult<BoxStream<'_, BackendResult<CopyRecord>>> {
204 self.inner.get_copy_records(paths, root, head)
205 }
206
207 fn gc(&self, index: &dyn Index, keep_newer: SystemTime) -> BackendResult<()> {
208 self.inner.gc(index, keep_newer)
209 }
210 }