0
I am using below code with flask and name is app1.py

app = Flask(__name__)
api = Api(app)
#Class to create fileshare

class FileShare(Resource):
    def post(self):
        # Get JSON arguments from Payload shared NAS path, directorname  groupname with read access and right access
        parentdir = request.json.get("shareUNCPath")
        dirname = request.json.get("shareFolderName")
        readGroup = request.json.get("readGroup")
        writeGroup = request.json.get("writeGroup")
        #Create shared path with UNC Path and sharefolder name
        path = os.path.join(parentdir, dirname)
        # Access the NAS path through NAS credentails
        class Impersonate:

            def __init__(self,user,password):
                #Update domain to access the shared NAS
                self.domain_name = "<domain>"
                self.user = user
                self.password = password
                logging.debug("Credentials Received: {} ".format(self.user))
            def logon(self):
                self.handle=win32security.LogonUser(self.user,self.domain_name,self.password,win32con.LOGON32_LOGON_INTERACTIVE,win32con.LOGON32_PROVIDER_DEFAULT)
                win32security.ImpersonateLoggedOnUser(self.handle)
                    
            def logoff(self):
                win32security.RevertToSelf() #terminates impersonation
                self.handle.Close() #guarantees cleanup
                    
        if __name__ == "__main__":
            #update username and password of the NAS path below within quotes
            a=Impersonate('username','password')
            try:
                a.logon() #Logon to NAS path with supplied credentails.
                try:
                    logging.debug("Sucessfully connectd to NAS  path {} ".format(parentdir))
                    # makedirs create directory recursively
                    os.makedirs(path)
                    #Set Permissions for the share folder for respective group
                    #Get the value of the groups stored in groupr and groupw as variables
                    try:
                        groupr, domain, type = win32security.LookupAccountName ("", readGroup)                        
                        sd = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION)
                        dacl = sd.GetSecurityDescriptorDacl()
                        #set permessions for readGroup with GENERIC_READ level permessions
                        dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_READ, groupr)
                    except win32security.error as e:
                        resp = Response('Sucessfully created fileshare {} however setting group permession for the group {} failed. Error  {}'.format(dirname, readGroup, e))                            
                        print (resp)
                        resp.status_code = 301
                        return resp
                    sd.SetSecurityDescriptorDacl(1, dacl, 0)
                    win32security.SetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION, sd)
                    try:    
                        groupw, domain, type = win32security.LookupAccountName ("", writeGroup)
                        sd = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION)
                        dacl = sd.GetSecurityDescriptorDacl()
                        #set permessions for writeGroup with GENERIC_WRITE level permessions
                        dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_WRITE, groupw)
                    except win32security.error as e:
                        resp = Response('Sucessfully created fileshare {} however setting group permession for the group {} failed. Error  {}'.format(dirname, writeGroup, e))                            
                        print (resp)
                        resp.status_code = 301
                        return resp    
                    sd.SetSecurityDescriptorDacl(1, dacl, 0)                    
                    win32security.SetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION, sd)
                    dacl = sd.GetSecurityDescriptorDacl()
                    cnt=dacl.GetAceCount()
                    for  i in range(0, cnt):
                        rev, access, usersid = dacl.GetAce(i)
                        user, group, type = win32security.LookupAccountSid('', usersid)
                        details = ('Group: {}/{}'.format(group, user), rev,  access)
                        #return ("Success Fileshare created: {} ".format(dirname))
                        resp = Response('Successfully created file share {}. Details {}'.format(dirname, details))
                        print (resp)
                        resp.status_code = 200
                        return resp
                except OSError as error:
                    print(error)
                    resp = Response('NAS operatoon failed, {} fileshare creation failed. Error - {}'.format(dirname, error))
                    print (resp)
                    resp.status_code = 300
                    return resp
                    #return ("Fileshare creation failed: {} ".format(dirname))
                            
            except Exception as error1:
                print(error1)
                logging.error("Failed to connect to NASa path{}, Error: {} ".format(parentdir, error1))
                resp = Response('NAS Operation failed, could not connect to UNC Shared path. Error{}'.format(error1))
                print (resp)
                resp.status_code = 201
                return resp
                a.logoff()




    api.add_resource(Create, '/test')
    if __name__ == "__main__":
        app.run(port=5001, host="0.0.0.0", use_reloader=True)

I have created waitress_server.py with below app1.py is called in below code. Just executing app1.py works fine, however when tried with waitress i see HTTP post through postman is received as NULL.

I am trying to use waitress for my production setup.

from waitress import serve
import app1
serve(app1.app, host='0.0.0.0', port=5001)

When i make a postman call i see response as null. Please help me if I am doing right.

goe
  • 337
  • 2
  • 14

1 Answers1

1

You have forgotten to return a Response in case of no given error.

Specifically this part:

if __name__ == "__main__":
    dacl.AddAccessAllowedAce(win32security.      ACL_REVISION,win32con.GENERIC_READ, groupr) #os.makedirs(path)
    

Is missing a return statement. The postman call returns None because functions return None by default.

You need to add a Response after the above mentioned code and also return it after all fallbacks.

PS: thank your for adding the code

Cobalt
  • 447
  • 5
  • 9
  • Thank you I made changes to script. When I call the postman for my running python app1.py works with postman. However when I call the same through waitress its not working. Any help plz. – goe Aug 20 '20 at 06:54