:module syntax support properties in apex_key

public_key and private_key properties support :module syntax so that the
key pairs can be dynamically created during the build, which is useful
for one-time keys.

Bug: 128960614
Test: m (apex_test amended)

Change-Id: I249b1d29f247784193b0d733a7b6a20274ece105
diff --git a/apex/key.go b/apex/key.go
index 07c3105..848e8ce 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -45,11 +45,11 @@
 }
 
 type apexKeyProperties struct {
-	// Path to the public key file in avbpubkey format. Installed to the device.
+	// Path or module to the public key file in avbpubkey format. Installed to the device.
 	// Base name of the file is used as the ID for the key.
-	Public_key *string
-	// Path to the private key file in pem format. Used to sign APEXs.
-	Private_key *string
+	Public_key *string `android:"path"`
+	// Path or module to the private key file in pem format. Used to sign APEXs.
+	Private_key *string `android:"path"`
 
 	// Whether this key is installable to one of the partitions. Defualt: true.
 	Installable *bool
@@ -68,15 +68,26 @@
 }
 
 func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
-	m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
-
-	// If not found, fall back to the local key pairs
-	if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+	// If the keys are from other modules (i.e. :module syntax) respect it.
+	// Otherwise, try to locate the key files in the default cert dir or
+	// in the local module dir
+	if android.SrcIsModule(String(m.properties.Public_key)) != "" {
 		m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+	} else {
+		m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
+		// If not found, fall back to the local key pairs
+		if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+			m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+		}
 	}
-	if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+
+	if android.SrcIsModule(String(m.properties.Private_key)) != "" {
 		m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+	} else {
+		m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
+		if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+			m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+		}
 	}
 
 	pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]