| 153 | } |
| 154 | |
| 155 | func (ukm *userKeyStore) AdminImportKey(ctx context.Context, req *enc.AdminImportKeyRequest) (*enc.AdminImportKeyResponse, error) { |
| 156 | dao, err := manager.Resolve[key.DAO](ctx) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | |
| 161 | var k *enc.Key |
| 162 | k, _, err = dao.GetKey(ctx, common.PydioSystemUsername, req.Key.ID) |
| 163 | if err != nil { |
| 164 | if !errors.Is(err, errors.StatusNotFound) { |
| 165 | return nil, err |
| 166 | } |
| 167 | } else if k != nil && !req.Override { |
| 168 | return nil, errors.WithMessagef(errors.StatusConflict, "Key already exists with [%s] id", req.Key.ID) |
| 169 | } |
| 170 | |
| 171 | log.Logger(ctx).Debug("Opening sealed key with imported password") |
| 172 | rsp := &enc.AdminImportKeyResponse{} |
| 173 | err = open(req.Key, []byte(req.StrPassword)) |
| 174 | if err != nil { |
| 175 | rsp.Success = false |
| 176 | return rsp, errors.WithMessagef(errors.StatusInternalServerError, "unable to decrypt %s for import, cause: %s", req.Key.ID, err.Error()) |
| 177 | } |
| 178 | |
| 179 | master, err := ukm.masterFromCache(ctx) |
| 180 | if err != nil { |
| 181 | return nil, err |
| 182 | } |
| 183 | log.Logger(ctx).Debug("Sealing with master key") |
| 184 | err = seal(req.Key, master) |
| 185 | if err != nil { |
| 186 | rsp.Success = false |
| 187 | return rsp, errors.WithMessagef(errors.StatusInternalServerError, "unable to encrypt %s.%s for export, cause: %s", common.PydioSystemUsername, req.Key.ID, err.Error()) |
| 188 | } |
| 189 | |
| 190 | if req.Key.CreationDate == 0 { |
| 191 | if k != nil { |
| 192 | req.Key.CreationDate = k.CreationDate |
| 193 | } else { |
| 194 | req.Key.CreationDate = int32(time.Now().Unix()) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if len(req.Key.Owner) == 0 { |
| 199 | req.Key.Owner = common.PydioSystemUsername |
| 200 | } |
| 201 | |
| 202 | log.Logger(ctx).Debug("Received request", zap.Any("Data", req)) |
| 203 | |
| 204 | log.Logger(ctx).Debug("Adding import info") |
| 205 | // We set import info |
| 206 | |
| 207 | if req.Key.Info == nil { |
| 208 | req.Key.Info = &enc.KeyInfo{} |
| 209 | } |
| 210 | |
| 211 | if req.Key.Info.Imports == nil { |
| 212 | req.Key.Info.Imports = []*enc.Import{} |