Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1278357%22%20slang%3D%22en-US%22%3EArchitecture%20Walkthrough%3A%20Building%20a%20multi-tenant%20Azure%20Architecture%20for%20a%20B2C%20scenario%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1278357%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERecently%2C%20I%20had%20a%20talk%20about%20a%20Mobile%20App%20that%20I%20have%20developed%20for%20personal%20needs.%20I%20initially%20planned%20to%20replay%20this%20talk%20but%20given%20the%20lockdown%20precaution%20measures%2C%20I%20thought%20it'd%20be%20better%20to%20make%20a%20blog%20post%20out%20of%20it.%20The%20idea%20is%20to%20walk%20you%20through%20the%20architecture%20behind%20this%20multi-tenant%20mobile%20app%20and%20explain%20the%20rationale%20behind%20every%20single%20choice.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20Use%20Case%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20wine%20lover%2C%20it%20was%20about%20time%20for%20me%20to%20build%20something%20new%20to%20manage%20my%20cellar.%20There%20are%20plenty%20of%20apps%20available%20on%20Google%20Play%20but%20I%20wanted%20to%20add%20my%20own%20bits%20(and%20wine%20skills)%20into%20the%20app%20to%20serve%20me%20exactly%20as%20I%20wanted.%20I%20built%20the%20app%20for%20myself%20and%20then%2C%20some%20friends%20asked%20me%20to%20get%20it%20too%20but...it%20was%20not%20multi-tenant...so%20I%20decided%20to%20make%20it%20multi-tenant%2C%20and%20although%20it%20is%20a%20hobby%20project%2C%20I%20wanted%20to%20build%20it%20in%20a%20professional%20way.%20That%20said%2C%20should%20you%20be%20excited%20by%20this%20blog%20post%2C%20don't%20try%20to%20look%20for%20the%20app%20as%20I%20haven't%20published%20it%20on%20Google%20Play%20as%20I%20don't%20want%20to%20pay%20for%20the%20Azure%20costs%20should%20many%20folks%20install%20it%2C%20so%20right%20now%20I%20just%20let%20my%20friends%20download%20the%20app%26nbsp%3B%3A).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20features%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20wanted%20an%20app%20that%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Eshows%20a%20dashboard%20with%20metrics%20such%20as%20the%20top%20countries%2C%20top%20appellations%2C%20number%20of%20wines%2C%20average%20consumption%20over%201%20month%20(important%20to%20see%20if%20you%20don't%20drink%20too%20much%20%3A))%2C%20average%20price%2C%20etc.%3C%2FLI%3E%0A%3CLI%3ESends%20a%20daily%20reminder%20about%20the%20next%20wine%20to%20drink.%20This%20is%20not%20a%20random%20selection%20but%20really%20based%20on%20wine%20age%2C%20region%2C%20color%20etc.%20to%20determine%20the%20best%20moment%20to%20drink%20it%2C%20that's%20the%20personal%20bits%20I%20wanted%20to%20add.%3C%2FLI%3E%0A%3CLI%3EAllows%20an%20easy%20capture%20of%20a%20new%20wine%20by%20making%20use%20of%20an%20OCR%20to%20extract%20wine%20information%20from%20the%20label.%20Indeed%2C%20I%20don't%20want%20to%20spend%2010%20minutes%20whenever%20I%20add%20a%20new%20wine%20to%20the%20cellar.%3C%2FLI%3E%0A%3CLI%3EHas%20a%20powerful%20search%20interface.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EThe%20Architecture%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EMy%20goal%20is%20to%20make%20sure%20that%20the%20shared%20backend%20remains%20healthy%20and%20secure.%20Any%20attempt%20from%20a%20malicious%20user%20to%20hack%20my%20backend%20should%20result%20in%20having%20that%20user%20just%20messing%20up%20with%20his%20own%20tenant%20while%20not%20impacting%20others.%20With%20that%20in%20mind%2C%20I%20came%20up%20with%20the%20following%20architecture%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22winecompanion.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F182197i4B85465FC7A61E26%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22winecompanion.png%22%20alt%3D%22winecompanion.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20rationale%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20first%20sight%2C%20it%20may%20look%20simple%20but%20it%20is%20a%20little%20more%20complicated%20than%20it%20seems.%20Let%20me%20explain%20all%20the%20bullet%20points%2C%20the%20implementation%20details%20will%20follow%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EThe%20choice%20of%20Azure%20AD%20B2C%20is%20a%20no%20brainer%20in%20this%20context%20since%20my%20target%20audience%20is%20lambda%20people.%20I%20currently%20only%20use%20local%20B2C%20users%20but%20could%20add%20social%20identities%20as%20supported%20IDPs.%20Azure%20AD%20B2C%20will%20also%20help%20me%20to%20secure%20my%20APIs%20with%20OpenID%20Connect%20(OIDC).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EThe%20mobile%20app%20itself%2C%20written%20in%20Xamarin%20Android%20and%20levaring%20MSAL%20for%20authenticating%20users.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EFront%20Door%20with%20WAF%20enabled.%20I%20chose%20Front%20Door%20because%20it%20is%20serverless%20and%20fully%20elastic%20and%20because%20it%20has%20a%20built-in%20WAF%20as%20well.%20Here%20I%20took%20the%20default%20ruleset%20protecting%20against%20OWASP%20top%2010.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EAll%20my%20backend%20services%20are%20frontended%20by%20facade%20APIs%20and%20the%20API%20gateway%20acts%20as%20a%20Policy%20Enforcement%20Point%20(PEP).%20Different%20policies%20such%20as%20Throttling%2C%20JWT%20token%20validation%2C%20...%20are%20enforced%20by%20the%20gateway.%20All%20custom%20built%20services%20have%20a%20network%20ACL%20restricted%20to%20the%20APIM%20gateway%20VIP%20and%20are%20double-checking%20sever-side%20the%20OIDC%20bits.%20Network%20ACLs%20are%20the%20reason%20why%20I%20do%20not%20use%20the%20Serverless%20offering%20of%20APIM%20since%20it%20does%20not%20come%20with%20a%20static%20VIP%26nbsp%3B%20nor%20even%20a%20dedicated%20ServiceTag%20for%20the%20time%20being.%20My%20APIM%20instance%20is%20restricted%20to%20my%20Frontdoor%20Service%20IPs%20using%20a%20custom%20policy.%20By%20the%20way%2C%20it%20is%20quite%20tedious%20since%20Service%20Tags%20are%20not%20incorporated%20in%20APIM...%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3ESensitive%20information%20is%20entirely%20stored%20in%20Azure%20Key%20Vault%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EThe%20Wine%20backend%20service%20is%20the%20main%20service%20consumed%20by%20the%20mobile%20app%20through%20its%20corresponding%20facade%20API.%20It%20is%20protected%20using%20OIDC%20and%20built%20in%20.NET%20Core%20and%20EF%20Core.%20It%20is%20based%20on%20the%20App%20Service%20building%20block.%20Managed%20Service%20Identity%20(MSI)%20is%20enabled%20to%20let%20the%20service%20grab%20the%20Cosmos%20DB%20keys.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EI%20am%20using%20Cosmos%20DB%2C%20not%20for%20fun%20but%20because%20in%20this%20particular%20use%20case%2C%20it%20is%20the%20perfect%20choice.%20More%20on%20this%20in%20the%20next%20section.%20I%20restrict%20the%20Cosmos%20DB%20to%20a%20subnet%20that%20is%20integrated%20with%20my%20App%20Service%20by%20leveraging%20Service%20Endpoints%20and%20Subnet%20Delegation.%20That%20way%2C%20I%20know%20that%20only%20my%20Wine%20backend%20service%20can%20talk%20to%20my%20Cosmos%20DB%20from%20a%20network%20perspective.%20I%20didn't%20want%20to%20use%20AKS%20nor%20an%20ASE%20because%20this%20would%20have%20been%20overkill%20in%20this%20context%20with%20a%20serious%20impact%20on%20costs%2C%20so%20that's%20why%20I%20just%20used%20this%20trick%20by%20having%20an%20empty%20VNET%20with%20subnet%20delegation%20enabled.%20Private%20link%20is%20not%20available%20through%20VNET%20delegation.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EI%20am%20using%20a%20Storage%20Account%20with%20a%20container%20per%20tenant.%20Each%20container%20stores%20the%20pictures%20of%20its%20corresponding%20tenant.%20The%20Mobile%20App%20uploads%20blobs%20directly%20to%20the%20target%20Storage%20Account%20to%20make%20it%20scalable%20as%20I%20don't%20want%20to%20introduce%20a%20man%20in%20the%20middle%20that%20could%20be%20a%20SPOF.%20The%20Blob%20Storage%20SDK%20also%20comes%20with%20awesome%20features%20that%20are%20not%20so%20easy%20to%20deal%20with%20when%20introducing%20a%20mediation%20API.%20Regarding%20the%20security%20aspects%2C%20whenever%20an%20upload%20takes%20place%2C%20the%20App%20requests%20a%20short-lived%20(5%20min)%20SAS%20token%20to%20the%20Wine%20API%2C%20scoped%20to%20its%20corresponding%20container%20with%20Append%20permission%20only.%20That%20way%2C%20anyone%20monitoring%20the%20network%20traffic%20would%20only%20see%20his%20own%20SAS%20and%20could%20only%20mess%20up%20with%20his%20own%20tenant.%20On%20top%20of%20it%2C%20the%20API%20operation%20returning%20the%20short-lived%20SAS%20token%20is%20throttled%20on%20a%20per%20tenant%20basis%20to%20prevent%20any%20abuse.%26nbsp%3B%20%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EThe%20OCR%20service%20is%20Azure's%20Computer%20Vision%20cognitive%20service.%20I%20put%20it%20behind%20a%20facade%20API%20in%20order%20to%20let%20the%20APIM%20gateway%20inject%20the%20shared%20API%20key.%20Since%20it%20is%20a%20shared%20(cross-tenant)%20key%2C%20I%20want%20to%20make%20sure%20not%20to%20disclose%20it%20to%20the%20mobile%20device.%26nbsp%3B%20This%20key%20is%20itself%20stored%20in%20Azure%20Key%20Vault%20and%20retrieved%20dynamically%20by%20the%20gateway%20through%20a%20policy%20that%20leverages%20MSI%2C%20set%20at%20APIM%20instance%20level%20with%20an%20Access%20Policy%20defined%20in%20Key%20Vault.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EThe%20subscription%20service%20serves%20a%20specific%20purpose%20which%20I'll%20detail%20in%20the%20next%20section%2C%20but%20in%20a%20nutshell%2C%20it%20allows%20the%20mobile%20app%20to%20retrieve%20its%20own%20tenant%20specific%20API%20keys%20by%20subscribing%20it%20to%20the%20APIM%20instance%20on%20the%20fly.%20So%2C%20the%20only%20think%20a%20mobile%20device%20ever%20sees%2C%20is%20always%20tenant-specific.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3ESome%20basic%20health%20check%20and%20monitoring%20is%20in%20place.%20Mostly%20using%20App%20Insights%20%2B%20Availability%20tests%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3BA%20Function%20App%20with%20two%20functions%20is%20used%20to%20send%20the%20daily%20reminder%20through%20Push%20Notifications.%20A%20more%20advanced%20health%20check%20is%20also%20performed%20through%20a%20scheduled%20function%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EPush%20notifications%20are%20made%20through%20App%20Center's%20Push%20service.%20Unfortunately%2C%20Microsoft%20will%20be%20retiring%20this%20service%20anytime%20soon...The%20alternative%20is%20to%20use%20Azure%20Notification%20Hub.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3ESo%2C%20the%20key%20aspects%20here%20are%3A%20I%20used%20the%20network%20here%20and%20there%20to%20maximize%20the%20security%20but%20I%20mostly%20rely%20on%20identity%20%26amp%3B%20MSI.%20I'm%20using%20Microsoft%20managed%20keys%20for%20encryption%20since%20data%20is%20certainly%20not%20%22classified%22%20information%20%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E.%26nbsp%3B%20The%20only%20sensitive%20information%20disclosed%20to%20the%20mobile%20device%20is%20tenant%20(user%20in%20this%20case)-specific.%20The%20Design%20is%20not%20Disaster%20Recovery%20ready%20but%20who%20cares%20for%20such%20an%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20implementation%20details%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20that%20we%20have%20seen%20the%20global%20architecture%2C%20let's%20zoom%20into%20some%20implementation%20details.%20I%20will%20only%20highlight%20the%20most%20important%20parts.%20The%20first%20and%20important%20step%20in%20terms%20of%20security%20is%20about%20the%20user%20registration%20whose%20the%20flow%20is%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22subscription%20flow.png%22%20style%3D%22width%3A%20608px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F181894i8FD849D314C829BC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22subscription%20flow.png%22%20alt%3D%22subscription%20flow.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20important%20bits%20here%20are%20about%20the%20retrieval%20of%20the%20tenant-specific%20API%20subscription%20keys.%20In%20my%20particular%20case%2C%20each%20user%20is%20a%20tenant%20and%20is%20the%20wine%20cellar%20owner.%20Therefore%2C%20upon%20registration%2C%20the%20system%20creates%20an%20API%20subscription%20on%20the%20fly%20and%20returns%20one%20of%20the%20generated%20subscription%20keys%20to%20the%20device%2C%20which%20stores%20it%20locally%20as%20long%20as%20the%20key%20is%20valid.%20The%20backend%20subscription%20service%20(item%2010%20in%20the%20architecture%20diagram)%20is%20protected%20by%20a%20facade%20API%20enforcing%20a%20JWT%20validation%20against%20our%20B2C%20directory.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20an%20example%20of%20such%20a%20JWT%20token%2C%20requested%20by%20the%20mobile%20app%2C%20to%20access%20any%20of%20my%20APIs%20on%20behalf%20of%20the%20logged%20in%20user%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22wine%20companion%20token.png%22%20style%3D%22width%3A%20960px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F181907iB8EFA43B8BFEDAA7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22wine%20companion%20token.png%22%20alt%3D%22wine%20companion%20token.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20token%20must%20contain%20the%20managecellar%20scope%20and%20of%20course%20be%20issued%20by%20my%20B2C%20directory%20with%20the%20valid%20audience%20(aka%20client%20app).%20Any%20request%20to%20the%20subscription%20service%20containing%20such%20a%20valid%20access%20token%20is%20forwarded%20to%20the%20subscription%20service.%20A%20throttling%20limit%20of%2010%20requests%2Fminute%2Fuser%20is%20set%20to%20avoid%20abuse%20of%20the%20subscription%20service%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CRATE-LIMIT-BY-KEY%20calls%3D%2210%22%20renewal-period%3D%2260%22%20counter-key%3D%22%40(context.Request.Headers.GetValueOrDefault(%22%20authorization%3D%22%22%3E%3C%2FRATE-LIMIT-BY-KEY%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eon%20the%20subject%20claim%2C%20highlighted%20in%20the%20above%20picture.%20So%2C%20here%20again%2C%20anyone%20monitoring%20the%20traffic%20would%20see%20his%20own%20access%20token%20but%20could%20only%20throttle%20himself%20should%20he%20try%20to%20play%20it%20the%20dirty%20way%2C%20while%20nor%20my%20backend%20service%2C%20nor%20other%20users%20will%20be%20impacted.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMore%20globally%2C%20the%20facade%20APIs%20are%20doing%20a%20lot%20of%20the%20heavy%20lifting%20and%20are%20organised%20this%20way%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22apis.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F182254i840FC45A55D739AD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22apis.png%22%20alt%3D%22apis.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3EAn%20API%20product%20applying%20multiple%20policies%20such%20as%20IP%20Filtering%20on%20the%20Front%20Door%20service%20and%20JWT%20token%20validation%20since%20none%20of%20my%20APIs%20can%20only%20be%20accessed%20using%20the%20subscription%20key%20only.%26nbsp%3B%20The%20product%20only%20contains%20the%20Wine%20%26amp%3B%20OCR%20APIs.%20Indeed%2C%20my%20subscription%20service's%20facade%20API%20is%20subscription-free%20since%20its%20primary%20purpose%20is%20to%20return%20the%20tenant-specific%20subscription%20keys.%20Therefore%2C%20I%20cannot%20reuse%20my%20product%20policies%20and%20have%20to%20replay%20them%20locally.%20The%20OCR%20facade%20API%20is%20first%20extracting%20the%20shared%20Cognitive%20Service%20API%20key%20from%20Key%20Vault%20and%20forwards%20the%20request%20to%20the%20Cognitive%20Service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELast%20but%20not%20least%3A%20Cosmos%20DB!%20Why%20did%20I%20choose%20Cosmos%20DB%3F%20I%20see%20many%20folks%20rushing%20to%20NoSQL%20in%20general%20and%20quickly%20facing%20issues%20because%20of%20poor%20up-front%20engineering.%20In%20this%20particular%20use%20case%2C%20Cosmos%20is%20perfect%20for%20the%20following%20reasons%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EOne%20of%20the%20rule%20of%20thumb%20of%20Cosmos%20DB%20is%20to%20evenly%20distribute%20the%20load.%20This%20means%20that%20each%20logical%20partition%20should%20roughly%20be%20about%20the%20same%20size%3C%2FLI%3E%0A%3CLI%3EAnother%20rule%20of%20thumb%20is%20to%20minimize%20cross-partition%20queries%20to%20avoid%20performance%20issues.%20So%2C%20the%20partition%20key%20should%20not%20only%20be%20a%20technical%20thing%20but%20should%20also%20ideally%20have%20a%20functional%20interest.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EA%20hard%20limit%20of%2020GB%20(for%20now)%20maximum%20partition%20size%20is%20applied%20to%20logical%20partitions.%20Therefore%2C%20one%20need%20to%20anticipate%20capacity%20on%20that%20level.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EMy%20scenario%20is%20perfectly%20in%20line%20with%20those%20three%20rules%20of%20thumb.%20I%20am%20using%20the%20Tenant%20(aka%20subject%20in%20my%20access%20token)%20as%20partition%20key.%26nbsp%3B%20I'm%20ok%20with%20rule%201%20because%20I%20can%20hardly%20imagine%20to%20have%20a%20user%20with%20a%20cellar%20of%201%20000%20000%20bottles%20and%20another%20one%20with%20only%201%20bottle.%20So%2C%20globally%2C%20the%20load%20will%20be%20evenly%20distributed%20across%20logical%20partitions.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'm%20ok%20with%20Rule%202%20because%20I'll%20include%20my%20partition%20key%20in%20every%20single%20query%20since%20I%20want%20to%20make%20sure%20that%20users%20can%20only%20see%20their%20own%20wines.%20At%20last%2C%20since%20I'm%20storing%20the%20pictures%20in%20a%20Storage%20Account%2C%20I%20can%20hardly%20imagine%20that%20a%20single%20cellar%20would%20need%20more%20than%2020GB....given%20my%20wine%20document%20is%20just%20about%20text%20metadata.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20this%20kind%20of%20approach%20is%20suitable%20for%20other%20B2C%20scenarios%2C%20that's%20why%20I%20wanted%20to%20share%20it!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1278357%22%20slang%3D%22en-US%22%3E%3CP%3EFind%20out%20how%20you%20can%20assemble%20a%20set%20of%20Azure%20Services%20in%20a%20modern%20multi-tenant%20B2C%20Mobile%20App%20scenario.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1278357%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eserverless%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESoftware%20Architecture%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

Hi,

 

Recently, I had a talk about a Mobile App that I have developed for personal needs. I initially planned to replay this talk but given the lockdown precaution measures, I thought it'd be better to make a blog post out of it. The idea is to walk you through the architecture behind this multi-tenant mobile app and explain the rationale behind every single choice. 

 

The Use Case

 

As a wine lover, it was about time for me to build something new to manage my cellar. There are plenty of apps available on Google Play but I wanted to add my own bits (and wine skills) into the app to serve me exactly as I wanted. I built the app for myself and then, some friends asked me to get it too but...it was not multi-tenant...so I decided to make it multi-tenant, and although it is a hobby project, I wanted to build it in a professional way. That said, should you be excited by this blog post, don't try to look for the app as I haven't published it on Google Play as I don't want to pay for the Azure costs should many folks install it, so right now I just let my friends download the app :).

 

The features

 

I wanted an app that:

  • Shows a dashboard with metrics such as the top countries, top appellations, number of wines, average consumption over 1 month (important to see if you don't drink too much :)), average price, etc.
  • Sends a daily reminder about the next wine to drink. This is not a random selection but really based on wine age, region, color etc. to determine the best moment to drink it, that's the personal bits I wanted to add.
  • Allows an easy capture of a new wine by making use of an OCR to extract wine information from the label. Indeed, I don't want to spend 10 minutes whenever I add a new wine to the cellar.
  • Has a powerful search interface.

   

The Architecture

My goal is to make sure that the shared backend remains healthy and secure. Any attempt from a malicious user to hack my backend should result in having that user just messing up with his own tenant while not impacting others. With that in mind, I came up with the following architecture:

 

winecompanion.png

The rationale

 

At first sight, it may look simple but it is a little more complicated than it seems. Let me explain all the numbered bullet points; the implementation details will follow:

  1. The choice of Azure AD B2C is a no brainer in this context, since my target audience is lambda people. I currently only use local B2C users but could add social identities as supported IDPs. Azure AD B2C will also help me to secure my APIs with OpenID Connect (OIDC).

  2. The mobile app itself, written in Xamarin Android and leveraging MSAL for authenticating users.

  3. Front Door with WAF enabled. I chose Front Door because it is serverless and fully elastic and because it has a built-in WAF as well. Here I took the default ruleset protecting against OWASP top 10.  Why not Azure Application Gateway? It would have also been a good choice but Front Door is cheaper (given my traffic) and since my backend pool is my APIM instance with a Public VIP, Front Door does the job pretty well.

  4. All my backend services are frontended by facade APIs and the API gateway acts as a Policy Enforcement Point (PEP). Different policies such as Throttling, JWT token validation, ... are enforced by the gateway. All custom built services have a network ACL restricted to the APIM gateway VIP and are double-checking sever-side the OIDC bits. Network ACLs are the reason why I do not use the Serverless offering of APIM since it does not come with a static VIP  nor even a dedicated ServiceTag for the time being. My APIM instance is restricted to my Frontdoor Service IPs using a custom policy. By the way, it is quite tedious since Service Tags are not incorporated in APIM...

  5. Sensitive information is entirely stored in Azure Key Vault.

  6. The Wine backend service is the main service consumed by the mobile app through its corresponding facade API. It is protected using OIDC and built in .NET Core and EF Core. It is based on the App Service building block. Managed Service Identity (MSI) is enabled to let the service grab the Cosmos DB keys.

  7. I am using Cosmos DB, not for fun but because in this particular use case, it is the perfect choice. More on this in the next section. I restrict the Cosmos DB to a subnet that is integrated with my App Service by leveraging Service Endpoints and Subnet Delegation. That way, I know that only my Wine backend service can talk to my Cosmos DB from a network perspective. I didn't want to use AKS nor an ASE because this would have been overkill in this context with a serious impact on costs, so that's why I just used this trick by having an empty VNET with subnet delegation enabled. Private link is not available through VNET delegation.

  8. I am using a Storage Account with a container per tenant. Each container stores the pictures of its corresponding tenant. The Mobile App uploads blobs directly to the target Storage Account to make it scalable as I don't want to introduce a man in the middle that could be a SPOF. The Blob Storage SDK also comes with awesome features that are not so easy to deal with when introducing a mediation API. Regarding the security aspects, whenever an upload takes place, the App requests a short-lived (5 min) SAS token to the Wine API, scoped to its corresponding container with Append permission only. That way, anyone monitoring the network traffic would only see his own SAS and could only mess up with his own tenant. On top of it, the API operation returning the short-lived SAS token is throttled on a per tenant basis to prevent any abuse.   

  9. The OCR service is Azure's Computer Vision cognitive service. I put it behind a facade API in order to let the APIM gateway inject the shared API key. Since it is a shared (cross-tenant) key, I want to make sure not to disclose it to the mobile device.  This key is itself stored in Azure Key Vault and retrieved dynamically by the gateway through a policy that leverages MSI, set at APIM instance level with an Access Policy defined in Key Vault.

  10. The subscription service serves a specific purpose which I'll detail in the next section, but in a nutshell, it allows the mobile app to retrieve its own tenant specific API keys by subscribing it to the APIM instance on the fly. So, the only think a mobile device ever sees, is always tenant-specific.

  11. Some basic health check and monitoring is in place. I'm mostly using App Insights + Availability tests.

  12. A Function App with two functions is used to send the daily reminder through Push Notifications. A more advanced health check is also performed through a scheduled function.

  13. Push notifications are made through App Center's Push service. Unfortunately, Microsoft will be retiring this service anytime soon...The alternative is to use Azure Notification Hub.

So, the key aspects here are: I used the network here and there to maximize the security but I mostly rely on identity & MSI. I'm using Microsoft managed keys for encryption since data is certainly not "classified" information :smile:.  The only sensitive information disclosed to the mobile device is tenant (user in this case)-specific. The Design is not Disaster Recovery ready but who cares for such an app.

 

 

The implementation details

 

Now that we have seen the global architecture, let's zoom into some implementation details. I will only highlight the most important parts. The first and important step in terms of security is about the user registration whose the flow is as follows:

 

subscription flow.png

 

The important bits here are about the retrieval of the tenant-specific API subscription keys. In my particular case, each user is a tenant and is the wine cellar owner. Therefore, upon registration, the system creates an API subscription on the fly and returns one of the generated subscription keys to the device, which stores it locally as long as the key is valid. The backend subscription service (item 10 in the architecture diagram) is protected by a facade API enforcing a JWT validation against our B2C directory. 

 

Here is an example of such a JWT token, requested by the mobile app, to access any of my APIs on behalf of the logged in user:

 

wine companion token.png

The token must contain the managecellar scope and of course be issued by my B2C directory with the valid audience (aka client app). Any request to the subscription service containing such a valid access token is forwarded to the subscription service. A throttling limit of 10 requests/minute/user is set to avoid abuse of the subscription service:

 

 

 

<rate-limit-by-key calls="10" 
            renewal-period="60" 
            counter-key="@(context.Request.Headers.GetValueOrDefault("Authorization","").AsJwt()?.Subject)" />

 

 

 

On the subject claim, highlighted in the above picture. So, here again, anyone monitoring the traffic would see his own access token but could only throttle himself should he try to play it the dirty way, while nor my backend service, nor other users will be impacted.

 

More globally, the facade APIs are doing a lot of the heavy lifting and are organised this way:

 

apis.png

An API product applying multiple policies such as IP Filtering on the Front Door service and JWT token validation since none of my APIs can only be accessed using the subscription key only.  The product only contains the Wine & OCR APIs. Indeed, my subscription service's facade API is subscription-free since its primary purpose is to return the tenant-specific subscription keys. Therefore, I cannot reuse my product policies and have to replay them locally. The OCR facade API is first extracting the shared Cognitive Service API key from Key Vault and forwards the request to the Cognitive Service.

 

Last but not least: Cosmos DB! Why did I choose Cosmos DB? I see many folks rushing to NoSQL and quickly facing issues because of poor up-front engineering. In this particular use case, Cosmos is perfect for the following reasons:

 

  1. One of the rule of thumb of Cosmos DB is to evenly distribute the load. This means that each logical partition should roughly be about the same size
  2. Another rule of thumb is to minimize cross-partition queries to avoid performance issues. So, the partition key should not only be a technical thing but should also ideally have a functional interest. 
  3. A hard limit of 20GB (for now) maximum partition size is applied to logical partitions. Therefore, one need to anticipate capacity on that level.

My scenario is perfectly in line with those three rules of thumb. I am using the Tenant (aka subject in my access token) as partition key.  I'm okay with rule 1 because I can hardly imagine to have a user with a cellar of 1 000 000 bottles and another one with only 1 bottle. So, globally, the load will be evenly distributed across logical partitions. 

 

I'm okay with Rule 2 because I'll include my partition key in every single query since I want to make sure that users can only see their own wines. At last, since I'm storing the pictures in a Storage Account, I can hardly imagine that a single cellar would need more than 20GB.... given my wine document is just about text metadata.

 

I think this kind of approach is suitable for other B2C scenarios; that's why I wanted to share it!