Salta ai contenuti
AggregaAggregaAggregaAggrega
Seleziona lingua: Italiano
Questa pagina è stata tradotta automaticamente. Leggi l’originale in inglese.

Distribuire un servizio che il catalogo non ha

Quando il catalogo non contiene alcun Component per un servizio cloud, la descrizione che il vendor stesso dà del tipo di risorsa di quel servizio può farne le veci. aggrega import legge la descrizione — lo schema che un vendor cloud pubblica per un tipo di risorsa — e ne mette in bozza due file: un Component che dichiara le impostazioni del tipo nei pilastri che Aggrega legge, e una Recipe che realizza il Component per un Provider scrivendo esattamente una risorsa di quel tipo. Rivedi le bozze, aggiungi le righe che le usano a un Aggregate e a una Release, ed esegui la build.

Vengono letti tre formati, ciascuno da un importer di base che lo serve:

FormatoLo schema che esportiIl Provider della Recipe
cloudformation-schemaun tipo di risorsa CloudFormation, ottenuto con aws cloudformation describe-type o dal file di schema del providercloudformation
terraform-schemalo schema di un provider Terraform o OpenTofu, ottenuto con terraform providers schema -jsonterraform
arm-schemail file di tipi Bicep di un resource provider Azure, types.jsonarm

Questa pagina percorre il caso Azure dall’inizio alla fine, poi mostra i casi CloudFormation e Terraform e una variante che mette in bozza soltanto una Recipe per un Component che il catalogo contiene già. I comandi e l’output delle parti Azure qui sotto provengono dall’esecuzione di una build della release del compilatore che include questi importer e il provider arm.

Nulla viene scaricato per tuo conto. Esporti lo schema con lo strumento del vendor stesso, quindi l’import legge un file che possiedi e non raggiunge alcun account cloud.

Esportare lo schema

Lo strumento di ciascun vendor scrive lo schema in un file. Esegui quello per il tuo cloud, in un workspace:

Terminal window
# CloudFormation: any registered type, third-party and private ones included.
aws cloudformation describe-type --type RESOURCE --type-name AWS::SQS::Queue > sqs-queue.json
# Terraform or OpenTofu: in a directory whose configuration requires the provider.
terraform init && terraform providers schema -json > schema.json
# Azure: no export is needed. Bicep's type files are published in the
# Azure/bicep-types-az repository, one for each resource provider and API
# version, at generated/<namespace>/<provider>/<apiVersion>/types.json.

Il caso Azure qui sotto usa il file per Microsoft.ManagedIdentity alla versione API 2024-11-30, conservato in una cartella chiamata msi con il nome che Bicep gli dà, types.json.

Il file di schema Azure e la sua licenza

types.json di Microsoft.ManagedIdentity alla versione 2024-11-30 è un file del repository Azure/bicep-types-az, copyright Microsoft Corporation, distribuito con la MIT License. Le descrizioni citate dalle bozze qui sotto sono di Microsoft, e a esse si applica l’avviso della licenza:

MIT License
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

Il workspace è una cartella che contiene un .aggrega-config.yaml con una chiave workspace, e la cartella msi con il file:

.aggrega-config.yaml
msi/types.json

Elencare i tipi da mettere in bozza

Un file di schema può contenere più di un tipo di risorsa, e l’import mette in bozza solo quelli che nomini. Elenca ciò che il file contiene:

Terminal window
jq -r '.[] | select(."$type" == "ResourceType") | .name' msi/types.json
Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30
Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials@2024-11-30
Microsoft.ManagedIdentity/identities@2024-11-30

Poi nomina quello che vuoi nell’impostazione resource_types della voce dell’importer in .aggrega-config.yaml. Ogni workspace serve già l’importer, quindi la voce serve soltanto per passare resource_types. Il suo uri nomina l’importer e la versione del compilatore, che aggrega version stampa; un importer riceve le proprie impostazioni tramite una voce di questo tipo, come descrive Quale importer trasforma una sorgente in bozze:

workspace: {}
default_registry: hub.aggrega.ai
plugins:
importer:
- uri: importer/arm-schema:<compiler version>
with:
settings:
resource_types:
- Microsoft.ManagedIdentity/userAssignedIdentities

I nomi vengono confrontati in modo esatto per CloudFormation (AWS::SQS::Queue) e Terraform (aws_sqs_queue), e senza distinguere maiuscole e minuscole per Azure, dove un nome può portare una versione API dopo una @. Un file che contiene esattamente un tipo non ha bisogno di un elenco. Un import di un file che contiene più tipi e nessun elenco si ferma e li nomina:

halt: the sources hold 3 resource types and the resource_types setting names none: write the types to draft in it, one of Microsoft.ManagedIdentity/userAssignedIdentities, Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials, Microsoft.ManagedIdentity/identities

Anche un tipo elencato che il file non contiene ferma l’import, nominando i nomi più vicini che contiene. Un arresto non mette in bozza nulla, quindi non viene scritto alcun file.

Eseguire l’importazione

Nomina la cartella, oppure il file:

Terminal window
aggrega import msi

Sullo standard output elenca ciò che ha scritto:

wrote catalog/component/imported/security/microsoft-managedidentity-userassignedidentities/0.1.0/component.yaml
wrote catalog/recipe/arm/microsoft-managedidentity-userassignedidentities/0.1.0/recipe.yaml
wrote .aggrega-import.json

Sullo standard error, dove <workspace> sta per la cartella in cui l’hai eseguita:

read as the arm-schema format, with drafted paths relative to <workspace>
mapped Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 (Microsoft.ManagedIdentity/userAssignedIdentities) to imported/security/microsoft-managedidentity-userassignedidentities as a draft, NONE at 0.05

aggrega import riconosce un file di tipi Bicep chiamato types.json, come li chiama Azure/bicep-types-az, e --format arm-schema nomina il formato per un file chiamato in altro modo. --format è anche il modo per importare un export di grandi dimensioni, come l’intero schema Terraform di un provider, senza che l’import lo riconosca prima.

La riga mapped dice che il tipo di risorsa è diventato una bozza a sé, senza alcun Component del catalogo vicino a esso. Le bozze vengono scritte sotto catalog/, una cartella che il workspace legge come catalogo, quindi si risolvono alla successiva validazione e build senza nulla da configurare. Il record .aggrega-import.json le elenca, così aggrega import --sync può aggiornarle a uno schema modificato senza sovrascrivere una tua modifica.

Che cosa dicono le bozze

Il Component, component.yaml, colloca ogni proprietà del tipo di risorsa in un pilastro, e un commento accanto a ciascuna chiave nomina la regola che l’ha collocata:

  • Una proprietà di sola lettura, che il cloud imposta e tu non puoi, non è un input. Un valore scalare diventa una chiave provides che un consumer può leggere.
  • Un segreto viene messo in bozza come riferimento al punto da cui proviene il suo valore, mai come valore, come descrive Segreti più sotto.
  • La collocazione, cioè dove vive la risorsa, come location in Azure, va in infra, il pilastro che un operatore fornisce per ogni ambiente.
  • L’identità, il nome proprio della risorsa, va in values, il pilastro che scrive lo sviluppatore.
  • Ogni altra proprietà obbligatoria va in infra.
  • Ogni altra proprietà scrivibile va sotto infra.overrides, una mappa aperta che conserva l’ortografia originale del vendor di ciascun nome.

Per questo tipo di risorsa il Component riporta:

apiVersion: aggrega.ai/v1beta1
kind: Component
metadata:
name: imported/security/microsoft-managedidentity-userassignedidentities
version: 0.1.0
description: "Draft Component of the resource type Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30, imported from a source in the arm-schema format."
labels:
component.aggrega.ai/type: resource
component.aggrega.ai/category: security
component.aggrega.ai/hosting: managed
spec:
schema:
values:
type: object
properties:
# identity: the resource's own name or identifier
# [vendor schema text] The resource name
name:
type: string
description: "[vendor schema text] The resource name"
required: true
additionalProperties: false
infra:
type: object
properties:
# placement: where the resource is deployed
# [vendor schema text] The geo-location where the resource lives
location:
type: string
description: "[vendor schema text] The geo-location where the resource lives"
required: true
# override: every writable property no other rule places, each under the vendor's own name
overrides:
type: object
description: "Writable settings of Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30, each under the vendor's own name, which the Recipe passes to the resource as written; its header lists the names it reads."
additionalProperties: true
additionalProperties: false
provides:
type: object
properties:
# read back: published under provides
# [vendor schema text] The resource id
id:
type: string
description: "[vendor schema text] The resource id"
# … client_id, principal_id and tenant_id, each read back the same way
additionalProperties: false

Ogni testo che la bozza prende dallo schema del vendor si apre con [vendor schema text], così puoi trovarli e riscriverli con parole tue prima che il Component lasci il tuo workspace. Il testo che l’import compone non porta alcun marcatore.

La Recipe, recipe.yaml, realizza il Component sul provider arm. Il suo commento di intestazione elenca ogni chiave che legge, con la sua regola e il suo tipo, e ciò che non legge:

spec:
# This Recipe realizes imported/security/microsoft-managedidentity-userassignedidentities with one resource of the type Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30. It reads the keys below, each with its type, its rule and the first sentence of the vendor's description; any other key under infra.overrides halts the build naming it.
# infra.location: string, required; placement: where the resource is deployed
# [… omitted: the first sentence of the vendor's description of the key above, marked as vendor schema text, as below each key]
# values.name: string, required; identity: the resource's own name or identifier
# [… omitted: the vendor's description of the key]
# infra.overrides.tags: object; override: writable, set under infra.overrides in the vendor's spelling
# [… omitted: the vendor's description of the key]
# infra.overrides.isolationScope: string; override: writable, set under infra.overrides in the vendor's spelling
# [… omitted: the vendor's description of the key, then a line saying template.yaml declares the resource under the symbolic name locals.symbol holds]
capability: arm
implements: imported/security/microsoft-managedidentity-userassignedidentities:0.1.0
locals:
# [… omitted: a comment saying what the symbolic name is made of]
symbol: "{{ capitalizedWords .context.instance_id }}"
provides:
# [… omitted: a comment saying each value is one whole ARM expression, evaluated when the template is deployed]
id: "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities'{{ range splitList \"/\" (toString (index .values \"name\")) }}, '{{ replace \"'\" \"''\" . }}'{{ end }})]"
client_id: "[reference('{{ .locals.symbol }}').clientId]"
# … principal_id and tenant_id the same way
assertions:
overrides_this_recipe_reads: |
# halts naming any key under infra.overrides the header does not list
artifacts:
template.yaml: |
# … the resource, written under the symbolic name locals.symbol holds

La Recipe scrive una risorsa, quella che il tipo nomina, e nient’altro: nessun resource group, nessuna rete, nessuna identità che tu non abbia dichiarato. Ogni valore di provides è un’espressione completa che Azure valuta quando il template viene distribuito, passata a un consumer come la stringa che è. Una chiave di infra.overrides che l’intestazione non elenca interrompe la build nominandola, così un’impostazione scritta male viene intercettata prima di distribuire qualsiasi cosa.

Rivedi entrambi i file prima di eseguire la build. Spostare un’impostazione da un pilastro a un altro è una modifica del Component e della lettura che ne fa la Recipe, e i file sono tuoi: nulla da cui la build dipenda è conservato altrove.

Usare le bozze in un Aggregate e in una Release

L’import non mette in bozza alcun Aggregate, alcun Manifest e alcuna Release, quindi le righe che usano il Component in bozza sono tue. In un Aggregate, aggregate.yaml, un component del tipo in bozza, con l’identità che il pilastro values contiene:

apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: identities
version: 1.0.0
spec:
components:
deploy-identity:
type: imported/security/microsoft-managedidentity-userassignedidentities:0.1.0
values:
name: deploy-identity

In un Manifest, manifest.yaml, una riga che dice quale Recipe realizza il tipo:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: identities
version: 1.0.0
spec:
mappings:
components:
type:
imported/security/microsoft-managedidentity-userassignedidentities:0.1.0: arm/microsoft-managedidentity-userassignedidentities:0.1.0

In una Release, release.yaml, il confine in cui finisce il component e il pilastro infra che la Recipe richiede, la collocazione:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: identities
version: 1.0.0
environment: production
spec:
aggregate: ./aggregate.yaml
manifest: ./manifest.yaml
boundaries:
azure:
directory: azure
components:
deploy-identity:
infra:
location: westeurope

Convalidare e compilare

Terminal window
aggrega validate --release release.yaml
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)

Esegui il pull di ciò da cui dipende la Release, poi la build:

Terminal window
aggrega pull --release release.yaml
aggrega build --release release.yaml
Pull complete: release.lock (entities=…, source_artifacts=…, external_inputs=…)
[… omitted: the build's elapsed time]
Build completed: 1 file written to <workspace>/dist

La build scrive dist/azure/arm/arm-template.json, l’unico template del confine azure, con una risorsa, con il nome simbolico che la sua istanza le assegna. Accanto ai membri propri della risorsa, la build registra sotto metadata quale Aggregate, quale Release e quale Recipe l’hanno prodotta:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"languageVersion": "2.0",
"resources": {
"DeployIdentity": {
"apiVersion": "2024-11-30",
"location": "westeurope",
"metadata": {
"aggrega.ai/provenance": {
"aggrega.ai/aggregate-name": "identities",
"aggrega.ai/boundary-path": "azure",
"aggrega.ai/compiler-version": "<compiler version>",
"aggrega.ai/component-id": "deploy-identity",
"aggrega.ai/environment": "production",
"aggrega.ai/provider": "arm",
"aggrega.ai/provider-version": "<compiler version>",
"aggrega.ai/recipe-uri": "microsoft-managedidentity-userassignedidentities:0.1.0"
}
},
"name": "deploy-identity",
"type": "Microsoft.ManagedIdentity/userAssignedIdentities"
}
}
}

Il template è ciò che consegni ad Azure; il provider arm descrive i comandi che lo distribuiscono a livello di resource group. Un tipo di risorsa scrivibile solo a livello di sottoscrizione o di management group viene messo in bozza come frammento per quel livello, e l’intestazione della sua Recipe nomina l’annotazione di confine che la Release imposta, come descrivono Distribuire a livello di sottoscrizione e Distribuire a livello di management group. Un tipo scrivibile solo a livello di tenant, o solo come estensione di un’altra risorsa, viene rifiutato per nome.

Segreti

Il valore di una proprietà segreta non viene mai scritto in una bozza e per essa non viene messo in bozza alcun default. Il modo in cui raggiunge la risorsa dipende dal provider:

  • Azure: un parametro securestring di primo livello del template, chiamato come l’istanza e la proprietà, che fornisci quando esegui la distribuzione. Un segreto che il vendor contrassegna come facoltativo è un parametro obbligatorio.
  • CloudFormation: una voce Parameters del template contrassegnata con NoEcho, chiamata allo stesso modo. Un segreto che il vendor contrassegna come facoltativo diventa un parametro obbligatorio.
  • Terraform: uno slot di Feature sul Component e una variabile del modulo contrassegnata con sensitive: true; il valore arriva tramite il segreto che la Release collega allo slot.

Solo una proprietà al livello superiore della risorsa diventa un segreto: su Azure una chiave della radice o un membro di properties. Un membro sensibile sotto di essa, come adminPassword dentro un osProfile di Azure o password dentro un blocco Terraform, non viene messo in bozza come segreto: la Release lo imposta come valore semplice dentro la chiave sotto cui viene messo in bozza il suo genitore, e l’import avvisa di ciascuno sotto constraint-dropped, nominandone il percorso e quella chiave.

Una proprietà CloudFormation viene letta come segreto quando il suo nome contiene una parola da credenziale — password, secret, token, credential, private key, API key o passphrase — e non termina con Arn, Id, Name o Ref. Questa lettura è una convenzione della bozza, e l’intestazione della Recipe contrassegna ogni proprietà che ha letto come segreto, così le confermi in fase di revisione.

Lo stesso import su CloudFormation e Terraform

I passaggi sono gli stessi; cambiano solo l’export e la forma della Recipe. I due casi qui sotto sono messi in bozza da schemi sintetici, il cui testo del vendor è omesso da ciò che segue.

CloudFormation

Terminal window
aws cloudformation describe-type --type RESOURCE --type-name AWS::Example::Ledger > ledger.json
aggrega import ledger.json

L’import scrive un Component e una Recipe, e un avviso per ogni vincolo del vendor che non può trasportare:

wrote catalog/component/imported/component/aws-example-ledger/0.1.0/component.yaml
wrote catalog/recipe/cloudformation/aws-example-ledger/0.1.0/recipe.yaml
wrote .aggrega-import.json
read as the cloudformation-schema format, with drafted paths relative to <workspace>
warn: the property "LedgerName" does not carry the vendor schema's pattern "…": the catalog reads a pattern as an RE2 regular expression, which refuses it: …
warn: the property "Rules[].Children[]" does not carry the vendor schema's shape below it: the vendor schema's definitions refer back to themselves there, so it is drafted as an open object
mapped AWS::Example::Ledger (AWS::Example::Ledger) to imported/component/aws-example-ledger as a draft, NONE at 0.0167

Il template.yaml della Recipe dichiara la risorsa con un id logico che l’istanza le assegna, con il segreto come parametro NoEcho e ogni chiave provides scritta come !Ref o !GetAtt, ripetuta sotto provides in modo che il provider cloudformation la proietti negli Outputs dello stack:

artifacts:
template.yaml: |
Parameters:
{{ .locals.logical_id }}MasterPassword:
Type: String
NoEcho: true
Resources:
{{ .locals.logical_id }}:
Type: AWS::Example::Ledger
Properties:
# … one line for each key the header lists
MasterPassword: !Ref {{ .locals.logical_id }}MasterPassword
provides:
arn: !GetAtt {{ .locals.logical_id }}.Arn

Terraform

Terminal window
terraform init && terraform providers schema -json > schema.json
aggrega import schema.json
wrote catalog/component/imported/component/aws-example-ledger/0.1.0/component.yaml
wrote catalog/recipe/terraform/aws-example-ledger/0.1.0/recipe.yaml
wrote catalog/recipe/terraform/aws-example-ledger/0.1.0/module/main.tf.json
wrote .aggrega-import.json

Un export Terraform nomina i provider che una configurazione richiede e nessuna loro versione, quindi l’import avvisa:

warn: no version of the provider hashicorp/aws is known, so the module drafted for aws_example_ledger requires it without a version constraint: set terraform.required_providers.aws.version in the module/main.tf.json beside the Recipe, then run aggrega pin on the Recipe

Una bozza Terraform è una Recipe con un modulo accanto. module/main.tf.json dichiara una variabile per ogni input, l’unica risorsa, this, con ogni argomento letto dalla propria variabile, e il provider che l’export nomina. Il spec.source della Recipe punta al modulo e il suo valuesMapping riempie ogni variabile dai pilastri del Component, scrivendo un input facoltativo solo dove la Release lo imposta. Fissa il modulo prima del primo pull, così il lock ne registra il digest:

Terminal window
aggrega pin catalog/recipe/terraform/aws-example-ledger/0.1.0/recipe.yaml
Pinned Recipe "aws-example-ledger": wrote <workspace>/catalog/recipe/terraform/aws-example-ledger/0.1.0/entity.lock (remote source pins: 0, imported file pins: 0, local source file pins: 1). Commit it beside the entity

Una Recipe non fissata interrompe un pull, nominando aggrega pin. Fissa di nuovo dopo ogni modifica del modulo.

Le righe di Aggregate, Manifest e Release sono quelle del caso Azure, con i nomi in bozza e l’infra proprio del provider: una Release Terraform nomina le chiavi di collocazione del provider, come region, dove il Component le dichiara.

Un Component che il catalogo ha già

Quando il catalogo contiene un Component per il servizio e manca solo la Recipe per il tuo provider, nomina quel Component nell’impostazione. L’import mette allora in bozza la sola Recipe, scritta rispetto ai pilastri del Component. Ogni voce di resource_types è un nome di tipo, oppure un oggetto con il tipo e il Component:

plugins:
importer:
# The entry passes resource_types; the importer is served without it.
- uri: importer/arm-schema:<compiler version>
with:
settings:
resource_types:
- type: Microsoft.ManagedIdentity/userAssignedIdentities
component: acme/security/identity

La Recipe prende il nome identity-microsoft-managedidentity-userassignedidentities, dal Component, così non prende mai il nome di una Recipe che il catalogo contiene già. Un Component del catalogo che dichiara una chiave values chiamata come una proprietà del tipo legge quella proprietà da values; una proprietà che il vendor richiede e per cui il Component non ha alcuna chiave viene letta da infra.overrides, con un’asserzione che interrompe la build finché la Release non la imposta. Ogni chiave che il Component fornisce e che corrisponde per nome a un attributo di sola lettura viene scritta. Per ogni altra, la bozza scrive un template che fallisce nominando la chiave, così una build si interrompe finché non la mappi, e l’import avvisa:

warn: the Component acme/security/identity provides principal_id, and no attribute Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 reads back carries its name, so the drafted Recipe's provides.principal_id halts the build until it is mapped: map it to an attribute Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30 reads back, one of id, properties.clientId, properties.principalId, properties.tenantId

Azure annida questo attributo sotto properties, quindi la riga da scrivere è quella che la bozza completa del tipo contiene:

provides:
principal_id: "[reference('{{ .locals.symbol }}').principalId]"

Scritta quella riga, la Release nomina il Component nel suo Aggregate e la Recipe nel suo Manifest come sopra, imposta la collocazione sotto infra.overrides.location, e si valida e viene costruita nello stesso template.

Un nome di cui il catalogo non contiene alcun Component viene messo in bozza come nuovo Component con quel nome, e l’import avvisa, così un nome che sbaglia l’ortografia di un Component non viene scambiato per uno nuovo.

Dove andare dopo

  • Importer di base descrive che cosa legge ciascun importer, come sceglie tra più tipi e tra più provider, la suddivisione per intero, che cosa mette in bozza ciascun target e le diagnostiche che i tre riportano.
  • Il provider arm descrive il template in cui viene unita una bozza Azure e i livelli a cui viene distribuita.
  • aggrega import è il riferimento completo del comando.