]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
15382c9fc31ac01ef793f4afb68837d52a005306
[ceph.git] /
1 import { Component, Input, OnChanges } from '@angular/core';
2
3 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
4
5 import * as xml2js from 'xml2js';
6
7 @Component({
8   selector: 'cd-rgw-bucket-details',
9   templateUrl: './rgw-bucket-details.component.html',
10   styleUrls: ['./rgw-bucket-details.component.scss']
11 })
12 export class RgwBucketDetailsComponent implements OnChanges {
13   @Input()
14   selection: any;
15
16   lifecycleFormat: 'json' | 'xml' = 'json';
17   aclPermissions: Record<string, string[]> = {};
18   replicationStatus = $localize`Disabled`;
19
20   constructor(private rgwBucketService: RgwBucketService) {}
21
22   ngOnChanges() {
23     if (this.selection) {
24       this.rgwBucketService.get(this.selection.bid).subscribe((bucket: object) => {
25         bucket['lock_retention_period_days'] = this.rgwBucketService.getLockDays(bucket);
26         this.selection = bucket;
27         if (this.lifecycleFormat === 'json' && !this.selection.lifecycle) {
28           this.selection.lifecycle = {};
29         }
30         this.aclPermissions = this.parseXmlAcl(this.selection.acl, this.selection.owner);
31         if (this.selection.replication?.['Rule']?.['Status']) {
32           this.replicationStatus = this.selection.replication?.['Rule']?.['Status'];
33         }
34       });
35     }
36   }
37
38   parseXmlAcl(xml: any, bucketOwner: string): Record<string, string[]> {
39     const parser = new xml2js.Parser({ explicitArray: false, trim: true });
40     let data: Record<string, string[]> = {
41       Owner: ['-'],
42       AllUsers: ['-'],
43       AuthenticatedUsers: ['-']
44     };
45     parser.parseString(xml, (err, result) => {
46       if (err) return null;
47
48       const xmlGrantees: any = result['AccessControlPolicy']['AccessControlList']['Grant'];
49       if (Array.isArray(xmlGrantees)) {
50         for (let i = 0; i < xmlGrantees.length; i++) {
51           const grantee = xmlGrantees[i];
52           if (grantee?.Grantee?.URI) {
53             const granteeGroup = grantee.Grantee.URI.split('/').pop();
54             if (data[granteeGroup].includes('-')) {
55               data[granteeGroup] = [grantee?.Permission];
56             } else {
57               data[granteeGroup].push(grantee?.Permission);
58             }
59           }
60           if (grantee?.Grantee?.ID && bucketOwner === grantee?.Grantee?.ID) {
61             data['Owner'] = grantee?.Permission;
62           }
63         }
64       } else {
65         if (xmlGrantees?.Grantee?.ID && bucketOwner === xmlGrantees?.Grantee?.ID) {
66           data['Owner'] = xmlGrantees?.Permission;
67         }
68       }
69     });
70     return data;
71   }
72 }