debian-forge-composer/internal/target/targetresult.go
Ondřej Budai 2e39d629a9 worker: add azure image upload target
This commit adds and implements org.osbuild.azure.image target.

Let's talk about the already implemented org.osbuild.azure target firstly:
The purpose of this target is to authenticate using the Azure Storage
credentials and upload the image file as a Page Blob. Page Blob is basically
an object in storage and it cannot be directly used to launch a VM. To achieve
that, you need to define an actual Azure Image with the Page Blob attached.

For the cloud API, we would like to create an actual Azure Image that is
immediately available for new VMs. The new target accomplishes it.
To achieve this, it must use a different authentication method: Azure OAuth.
The other important difference is that currently, the credentials are stored
on the worker and not in target options. This should lead to better security
because we don't send the credentials over network. In the future, we would
like to have credential-less setup using workers in Azure with the right
IAM policies applied but this requires more investigation and is not
implemented in this commit.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2021-03-06 15:40:48 +00:00

60 lines
1.3 KiB
Go

package target
import (
"encoding/json"
"fmt"
)
type TargetResult struct {
Name string `json:"name"`
Options TargetResultOptions `json:"options"`
}
func newTargetResult(name string, options TargetResultOptions) *TargetResult {
return &TargetResult{
Name: name,
Options: options,
}
}
type TargetResultOptions interface {
isTargetResultOptions()
}
type rawTargetResult struct {
Name string `json:"name"`
Options json.RawMessage `json:"options"`
}
func (targetResult *TargetResult) UnmarshalJSON(data []byte) error {
var rawTR rawTargetResult
err := json.Unmarshal(data, &rawTR)
if err != nil {
return err
}
options, err := UnmarshalTargetResultOptions(rawTR.Name, rawTR.Options)
if err != nil {
return err
}
targetResult.Name = rawTR.Name
targetResult.Options = options
return nil
}
func UnmarshalTargetResultOptions(trName string, rawOptions json.RawMessage) (TargetResultOptions, error) {
var options TargetResultOptions
switch trName {
case "org.osbuild.aws":
options = new(AWSTargetResultOptions)
case "org.osbuild.gcp":
options = new(GCPTargetResultOptions)
case "org.osbuild.azure.image":
options = new(AzureImageTargetResultOptions)
default:
return nil, fmt.Errorf("Unexpected target result name: %s", trName)
}
err := json.Unmarshal(rawOptions, options)
return options, err
}