fix: make client integration kit self-contained and clarify capabilities
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2
client/requirements.txt
Normal file
2
client/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
cryptography>=41
|
||||
httpx>=0.25,<1
|
||||
@@ -8,11 +8,11 @@ import { UpdateClient, UpdateError, verifyCheck } from './update-sdk.mjs';
|
||||
|
||||
const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
|
||||
const now = Date.now();
|
||||
const payload = {version:'update-v1',instance_id:'i1',product_id:1,customer_id:2,current_version:'1.0',platform:'linux',arch:'x64',channel:'stable',server_time:new Date(now).toISOString().replace('.000Z','Z'),expires_at:new Date(now+600000).toISOString().replace('.000Z','Z'),policy_revision:3,policy_enabled:true,recommended_version:'2.0',minimum_supported_version:'1.0',enforce_after:null,mandatory:false,required_now:false,update_available:true,download_allowed:true,release_id:7,sha256:createHash('sha256').update('good').digest('hex'),size_bytes:4,message:''};
|
||||
const payload = {version:'update-v1',instance_id:'i1',product_id:1,customer_id:2,current_version:'1.0.0',platform:'linux',arch:'x64',channel:'stable',server_time:new Date(now).toISOString().replace('.000Z','Z'),expires_at:new Date(now+600000).toISOString().replace('.000Z','Z'),policy_revision:3,policy_enabled:true,recommended_version:'2.0.0',minimum_supported_version:'1.0.0',enforce_after:'',mandatory:false,required_now:false,update_available:true,download_allowed:true,release_id:7,sha256:createHash('sha256').update('good').digest('hex'),size_bytes:4,message:''};
|
||||
function envelope(p = payload) { const raw = Buffer.from(JSON.stringify(p)); return {payload:raw.toString('base64').replace(/=+$/,''),signature:sign('sha256',raw,privateKey).toString('base64').replace(/=+$/,''),key_id:'k1'}; }
|
||||
const keys = {k1:publicKey.export({type:'spki',format:'pem'})};
|
||||
const signed = envelope();
|
||||
verifyCheck(signed, keys, 'i1', {now,expectedPlatform:'linux',expectedArch:'x64',expectedChannel:'stable',expectedCurrentVersion:'1.0'});
|
||||
verifyCheck(signed, keys, 'i1', {now,expectedPlatform:'linux',expectedArch:'x64',expectedChannel:'stable',expectedCurrentVersion:'1.0.0'});
|
||||
const tampered = {...signed, payload:Buffer.from('{"version":"update-v1"}').toString('base64').replace(/=+$/,'')};
|
||||
assert.throws(() => verifyCheck(tampered, keys, 'i1', {now}), UpdateError);
|
||||
assert.throws(() => verifyCheck(signed, keys, 'wrong', {now}), UpdateError);
|
||||
@@ -33,5 +33,5 @@ const state = {load:()=>persisted,saveAtomic:value=>{persisted=value;}};
|
||||
const fetchImpl = async () => Response.json({data:signed});
|
||||
const client = new UpdateClient({baseUrl:'https://license.example',instanceId:'i1',secret:'secret',publicKeys:keys,state,fetchImpl,clock:()=>now});
|
||||
persisted={scope:JSON.stringify(['i1','windows','x64','stable']),policy_revision:99};
|
||||
assert.equal((await client.check('1.0','linux','x64','stable')).payload.policy_revision,3);
|
||||
assert.equal((await client.check('1.0.0','linux','x64','stable')).payload.policy_revision,3);
|
||||
console.log('Node update SDK negative tests passed');
|
||||
|
||||
@@ -25,7 +25,7 @@ class SDKTests(unittest.TestCase):
|
||||
self.public = self.private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()
|
||||
self.now = __import__('time').time()
|
||||
stamp = datetime.fromtimestamp(self.now, timezone.utc).isoformat().replace('+00:00', 'Z')
|
||||
self.payload = {"version":"update-v1","instance_id":"i1","product_id":1,"customer_id":2,"current_version":"1.0","platform":"linux","arch":"x64","channel":"stable","server_time":stamp,"expires_at":datetime.fromtimestamp(self.now+600, timezone.utc).isoformat().replace('+00:00','Z'),"policy_revision":3,"policy_enabled":True,"recommended_version":"2.0","minimum_supported_version":"1.0","enforce_after":None,"mandatory":False,"required_now":False,"update_available":True,"download_allowed":True,"release_id":7,"sha256":hashlib.sha256(b'good').hexdigest(),"size_bytes":4,"message":""}
|
||||
self.payload = {"version":"update-v1","instance_id":"i1","product_id":1,"customer_id":2,"current_version":"1.0.0","platform":"linux","arch":"x64","channel":"stable","server_time":stamp,"expires_at":datetime.fromtimestamp(self.now+600, timezone.utc).isoformat().replace('+00:00','Z'),"policy_revision":3,"policy_enabled":True,"recommended_version":"2.0.0","minimum_supported_version":"1.0.0","enforce_after":"","mandatory":False,"required_now":False,"update_available":True,"download_allowed":True,"release_id":7,"sha256":hashlib.sha256(b'good').hexdigest(),"size_bytes":4,"message":""}
|
||||
self.envelope = self.sign(self.payload)
|
||||
|
||||
def sign(self, payload):
|
||||
@@ -34,7 +34,7 @@ class SDKTests(unittest.TestCase):
|
||||
return {"payload":base64.b64encode(raw).decode().rstrip('='),"signature":base64.b64encode(sig).decode().rstrip('='),"key_id":"k1"}
|
||||
|
||||
def test_signature_context_expiry_and_rollback(self):
|
||||
verify_check(self.envelope, {"k1":self.public}, "i1", now=self.now, expected_platform="linux", expected_arch="x64", expected_channel="stable", expected_current_version="1.0")
|
||||
verify_check(self.envelope, {"k1":self.public}, "i1", now=self.now, expected_platform="linux", expected_arch="x64", expected_channel="stable", expected_current_version="1.0.0")
|
||||
tampered = dict(self.envelope); tampered["payload"] = base64.b64encode(b'{"version":"update-v1"}').decode().rstrip('=')
|
||||
with self.assertRaises(UpdateError): verify_check(tampered, {"k1":self.public}, "i1", now=self.now)
|
||||
with self.assertRaises(UpdateError): verify_check(self.envelope, {"k1":self.public}, "wrong", now=self.now)
|
||||
@@ -65,7 +65,7 @@ class SDKTests(unittest.TestCase):
|
||||
def test_scoped_revision_does_not_leak_between_platforms(self):
|
||||
state = MemoryState({"scope": '["i1","windows","x64","stable"]', "policy_revision":99})
|
||||
sdk = self.client(lambda req: httpx.Response(200, json={"data":self.envelope}), state)
|
||||
result = sdk.check('1.0','linux','x64','stable')
|
||||
result = sdk.check('1.0.0','linux','x64','stable')
|
||||
self.assertEqual(result.payload['policy_revision'], 3)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Reference client for the signed update-v1 contract.
|
||||
|
||||
Requires Python 3.11 and the ``cryptography`` package. No installer is executed
|
||||
Requires Python 3.11+ and the ``cryptography`` and ``httpx`` packages. No installer is executed
|
||||
by this module; callers inject an installer after verification.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
Reference in New Issue
Block a user