diff --git a/.env.local b/.env.local index 203d6120..8895b89b 100644 --- a/.env.local +++ b/.env.local @@ -95,4 +95,30 @@ AZURE_PEDW_CONTAINER = "pedwapplications" AZURE_PEDW_QUEUE_ENDPOINT = https://$AZURE_STORAGE_ACCOUNT_NAME.queue.core.windows.net //gov.notify -NOTIFY_API_KEY = pedwnextauthkey-90f93710-9dae-423e-aea1-11eccd0dc132-0c9469d9-d671-444d-9e9d-3e5a62001043 \ No newline at end of file +NOTIFY_API_KEY = pedwnextauthkey-90f93710-9dae-423e-aea1-11eccd0dc132-0c9469d9-d671-444d-9e9d-3e5a62001043 + + +# //Bastion access to vnet vms +# [13:56] Suter, Matthew (COOG - DDAT - IT Services) +# Login is PEDWAdmin +# [13:57] Suter, Matthew (COOG - DDAT - IT Services) +# Pwd is -PK2_NQ-HuD2AFp + + +# [13:41] Suter, Matthew (COOG - DDAT - IT Services) +# so the admin login is PEDWSqlAdminPP +# [13:41] Suter, Matthew (COOG - DDAT - IT Services) +# password is rvJDj4ekfGSgwQpWTA38tDVJvFfSMbvmNGxd3YcII0M= + + + +# @Microsoft.KeyVault(SecretUri=https://test-pedw-kv.vault.azure.net/secrets/PEDW-FrontEnd-Test-AppReg-Secrate/) + + +# [12:24] Thompson, Phil (COOG - Planning & Environment Decisions Wales) +# {"pinswg_name":"48181 Planning Appeal E PROOF8.pdf","pinswg_DocumentIds@odata.bind":"/incidents(6CD13EF8-5BC9-ED11-A997-00224800E98C)","pinswg_isharedocumentlocations":"846040009","pinswg_isharedocumentclassification":"846040000","pinswg_uploadurl":"https://pedwinternalprod.blob.core.windows.net/crm-file-upload/6CD13EF8-5BC9-ED11-A997-00224800E98C/48181 Planning Appeal E PROOF8.pdf"} +# [12:25] Thompson, Phil (COOG - Planning & Environment Decisions Wales) +# https://crm2016.llc.gov.wales/PEDW/api/data/v8.2/pinswg_documents + + +# DATABASE_URL='sqlserver://pp-pedw-sql.database.windows.net:1433;database=PEDWPreProd;user=pp-pedw-ukw;password=jscw4+896w9YaiXruOA3mtpNkio1oUZxBZcFJ9Awdz4=;encrypt=true;trustservercertificate=false;hostnameincertificate=*.database.windows.net;logintimeout=30;authentication=SqlPassword' \ No newline at end of file diff --git a/actions/azurestorage.js b/actions/azurestorage.js index 67dae4d6..06f5593e 100644 --- a/actions/azurestorage.js +++ b/actions/azurestorage.js @@ -34,6 +34,8 @@ const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME; export const createContainerSas = async (containerName) => { // Get environment variables + containerName.length > 0; + // Best practice: create time limits const TEN_MINUTES = 10 * 60 * 1000; const NOW = new Date(); @@ -68,7 +70,7 @@ export const createContainerSas = async (containerName) => { expiresOn: TEN_MINUTES_AFTER_NOW, }; - //conLogJSON.stringify(sasOptions)); + console.log(JSON.stringify(sasOptions)); const sasToken = generateBlobSASQueryParameters( sasOptions, @@ -1116,12 +1118,13 @@ export const getAllProgressBlobs = async (containerName) => { }); } - //console.log( - // "\n////////////////////////////\n", - // "blobObj:", - // blobObj, - // "\n////////////////////////////" - // ); + console.log( + "\n////////////////////////////\n", + sasUrl, + "blobObj:", + blobObj, + "\n////////////////////////////" + ); return blobObj; }; @@ -1332,3 +1335,84 @@ export const createRepCompleteMessage = async ( return sendMessageResponse; }; + +export const listContainers = async ( + blobServiceClient, + containerNamePrefix +) => { + const options = { + includeDeleted: false, + includeMetadata: true, + includeSystem: true, + prefix: containerNamePrefix, + }; + console.log( + "\n\n\n=================================================\n", + "Showing storage account contents:\n\n" + ); + for await (const containerItem of blobServiceClient.listContainers( + options + )) { + // ContainerItem + console.log(`For-await list: ${containerItem.name}`); + + // ContainerClient + const containerClient = blobServiceClient.getContainerClient( + containerItem.name + ); + + await listBlobHierarchical(containerClient).catch((error) => + consoleLogger(error) + ); + // ... do something with container + } + console.log("\n=================================================\n\n\n\n"); +}; + +export const listBlobHierarchical = async ( + containerClient, + virtualHierarchyDelimiter = "/" +) => { + // page size - artificially low as example + const maxPageSize = 2; + + // some options for filtering list + const listOptions = { + includeCopy: false, // include metadata from previous copies + includeDeleted: false, // include deleted blobs + includeDeletedWithVersions: false, // include deleted blobs with versions + includeLegalHold: false, // include legal hold + includeMetadata: true, // include custom metadata + includeSnapshots: false, // include snapshots + includeTags: true, // include indexable tags + includeUncommitedBlobs: false, // include uncommitted blobs + includeVersions: false, // include all blob version + prefix: "", // filter by blob name prefix + }; + + let i = 1; + console.log(`Folder ${virtualHierarchyDelimiter}`); + + for await (const response of containerClient + .listBlobsByHierarchy(virtualHierarchyDelimiter, listOptions) + .byPage({ maxPageSize })) { + console.log(` Blob number ${i++}`); + const segment = response.segment; + + if (segment.blobPrefixes) { + // Do something with each virtual folder + for await (const prefix of segment.blobPrefixes) { + // build new virtualHierarchyDelimiter from current and next + await listBlobHierarchical( + containerClient, + `${virtualHierarchyDelimiter}${prefix.name}` + ); + } + } + + for (const blob of response.segment.blobItems) { + // Do something with each blob + console.log(`\tBlobItem: name - ${blob.name}`); + } + } +}; diff --git a/pages/dbcheck.js b/pages/dbcheck.js new file mode 100644 index 00000000..95f55d34 --- /dev/null +++ b/pages/dbcheck.js @@ -0,0 +1,74 @@ +import { DefaultAzureCredential } from "@azure/identity"; +import { BlobServiceClient, ContainerClient } from "@azure/storage-blob"; +import { v4 as uuidv4 } from "uuid"; +import { + createContainerSas, + listContainers, + listBlobHierarchical, +} from "../actions/azurestorage"; + +import { PrismaClient } from "@prisma/client"; + +import { consoleLogger, getIP } from "../actions"; + +const ShowStorage = (props) => { + const { container } = props; + + return